Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

VS 2010 Compiling scripts.

$
0
0
Hello everyone.

I'm developing some files for a game, and in this game there are NPC's (Non-playable-characters). These NPCs, you're able to talk and interact with. They all require scripts to perform (The scripts are representing their actions and what they'll say). My friend is also making the game files, in C#. He used the exact same method I'm using (I converted his..) to compile the scripts for the NPC's, and his scripts are in ".s" (C#). This is the function that loads the scripts.

vb Code:
  1. Public Sub MakeAvailableScripts(ByRef pCharacter As Character)
  2.         If mAvailableNPCScripts Is Nothing Then
  3.             mAvailableNPCScripts = New Dictionary(Of String, INpcScript)
  4.         Else
  5.             mAvailableNPCScripts.Clear()
  6.         End If
  7.         Dim results As CompilerResults
  8.  
  9.         Dim FilesNeedingRecompiling As List(Of String) = ScriptDataChecker.GetFilesNeedingRecompiling()
  10.  
  11.         For Each filename As String In Directory.GetFiles("C:\Scripts", "*.vb")
  12.             Dim fi As FileInfo = New FileInfo(filename)
  13.  
  14.             results = Scripting.CompileScript(filename)
  15.             If results.Errors.Count > 0 Then
  16.                 If pCharacter IsNot Nothing Then
  17.                     MessagePackets.SendMessage(pCharacter, 1, "Couldn't compile the file (" & fi.Name & ") correctly.")
  18.                     For Each _error As CompilerError In results.Errors
  19.                         MessagePackets.SendMessage(pCharacter, 1, "Line " & _error.Line.ToString & ", Column " & _error.Column.ToString & ": " & _error.ErrorText & ".")
  20.                     Next
  21.                 Else
  22.                     Logger.writeLog(LogTypes.Error_, "Couldn't compile the file (" & fi.Name & ") correctly.")
  23.                     For Each _error As CompilerError In results.Errors
  24.                         Logger.writeLog(LogTypes.Error_, "Line " & _error.Line.ToString & ", Column " & _error.Column.ToString & ": " & _error.ErrorText & ".")
  25.                     Next
  26.                 End If
  27.  
  28.             Else
  29.                 If pCharacter IsNot Nothing Then
  30.                     MessagePackets.SendMessage(pCharacter, 1, "Compiled " & fi.Name & "!")
  31.                 Else
  32.                     Logger.writeLog(LogTypes.Info, "Compiled {0}!", fi.Name)
  33.                 End If
  34.                 mAvailableNPCScripts.Add(fi.Name.Replace(".vb", ""), CType(Scripting.FindInterface(results.CompiledAssembly, "INpcScript"), INpcScript))
  35.             End If
  36.  
  37.             fi = Nothing
  38.         Next
  39.  
  40.         ScriptDataChecker.GenerateNewScriptHashes()
  41.     End Sub

So as you can see, it basically reads all the files named ".vb" (His were named ".s", I changed it to ".vb", thought it's the most logical thing to do since I'm converting it to VB.NET). It makes a new dictionairy of (String, INpcScript) called "mAvailableNPCScripts" (The scripts which are done compiling), and if there are errors - it writes them to the Logger.

Theres are the functions from my Scripting class (Also, used his).

vb Code:
  1. Public Shared Function CompileScript(Source As String) As CompilerResults
  2.         Dim compiler As CodeDomProvider = CodeDomProvider.CreateProvider("CSharp")
  3.         Dim parms As New CompilerParameters
  4.  
  5.         parms.GenerateExecutable = False
  6.         parms.GenerateInMemory = True
  7.         parms.IncludeDebugInformation = False
  8.         parms.ReferencedAssemblies.Add("System.dll")
  9.         parms.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location)
  10.  
  11.         Return compiler.CompileAssemblyFromFile(parms, Source)
  12.     End Function
  13.  
  14.     Public Shared Function FindInterface(DLL As System.Reflection.Assembly, InterfaceName As String) As Object
  15.         ' Loop through types looking for one that implements the given interface
  16.         For Each t As Type In DLL.GetTypes()
  17.             If t.GetInterface(InterfaceName, True) IsNot Nothing Then
  18.                 Return DLL.CreateInstance(t.FullName)
  19.             End If
  20.         Next
  21.  
  22.         Return Nothing
  23.     End Function

So, I went to try it out and make a basic script. This was my result:

vb Code:
  1. Public Class NpcScript
  2.     Inherits INpcScript
  3.  
  4.     Public mHost As IHost
  5.  
  6.     Public Sub Init(host As IHost)
  7.         mHost = host
  8.     End Sub
  9.  
  10.     Public Sub Run(character As Character, State As Byte, Answer As Byte, StringAnswer As String, IntegerAnswer As Integer)
  11.         If State = 0 Then
  12.             mHost.SendOK("It is a fine day for laundry. Wouldn't you agree?")
  13.         Else
  14.             mHost.Stop_()
  15.         End If
  16.     End Sub
  17. End Class

As you can see, it inherits the class mHost and INpcScript which have basic functions of Run, Init, SendOk, Stop, etc.. However, I'm getting the following error:

Code:

Line 1, Coulmn 1: A namespace cannot directly contain members such as fields or methods.
Is it ok that I converted it to .vb files and then compiling it that way? How exactly can I fix my error and make it a compileable script? Thank you everybody, I hope I provided enough information - if not, please tell me what to add.

Have a nice day!

Edit: I uploaded a .rar file containing 2 more classes: INpcScript and NpcChatSession, if anyone wants to to take a look at them, too.
http://hostr.co/5KcN87Z2DLPK

If anyone's not getting how it works: The script gets decompiled. When the player chooses a NPC, it assigns a new NpcChatSession of the Npc he chose and then it assigns all the functions of the compiled script (Run, Stop, etc)..

Viewing all articles
Browse latest Browse all 27513

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>