-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added Roslyn to the project. - Added an example of parsing game logic settings from a .csx file. - Converted extended NPC facilities into scripting .csx files for each NPC.
- Loading branch information
1 parent
a0b4c4d
commit 0448cc8
Showing
15 changed files
with
411 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace Arrowgene.Ddon.GameServer.Scripting | ||
{ | ||
public class DdonLibrary | ||
{ | ||
public DdonLibrary() | ||
{ | ||
} | ||
|
||
public static uint GetValue() | ||
{ | ||
return 1; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Arrowgene.Ddon.GameServer/Scripting/INpcExtendedFacility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Arrowgene.Ddon.Shared.Entity.PacketStructure; | ||
using Arrowgene.Ddon.Shared.Model; | ||
|
||
namespace Arrowgene.Ddon.GameServer.Scripting | ||
{ | ||
public abstract class INpcExtendedFacility | ||
{ | ||
/// <summary> | ||
/// NPC ID associated with the extended options. | ||
/// </summary> | ||
public NpcId NpcId { get; protected set; } | ||
|
||
/// <summary> | ||
/// Gets extended menu options for the NPC. | ||
/// </summary> | ||
/// <param name="server"></param> | ||
/// <param name="client"></param> | ||
/// <param name="result">The result object for the extended NPC options</param> | ||
public abstract void GetExtendedOptions(DdonGameServer server, GameClient client, S2CNpcGetNpcExtendedFacilityRes result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using Arrowgene.Ddon.Server; | ||
using Arrowgene.Ddon.Shared; | ||
using Arrowgene.Ddon.Shared.Model; | ||
using Arrowgene.Logging; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Scripting; | ||
using Microsoft.CodeAnalysis.Scripting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Arrowgene.Ddon.GameServer.Scripting | ||
{ | ||
public class Globals | ||
{ | ||
public DdonGameServer Server { get; set; } | ||
public GameLogicSetting GameLogicSetting { get; set; } | ||
} | ||
|
||
public class ScriptManager | ||
{ | ||
public string ScriptsRoot { get; private set; } | ||
public string SettingsPath { get; private set; } | ||
public string ExtendedFacilitiesPath { get; private set; } | ||
|
||
public Dictionary<NpcId, INpcExtendedFacility> NpcExtendedFacilities; | ||
|
||
public Script Settings { | ||
get { | ||
return CompiledScripts["Settings"]; | ||
} | ||
set | ||
{ | ||
CompiledScripts["Settings"] = value; | ||
} | ||
} | ||
|
||
private Dictionary<string, Script> CompiledScripts; | ||
private static readonly ServerLogger Logger = LogProvider.Logger<ServerLogger>(typeof(ScriptManager)); | ||
private Globals Globals { get; } | ||
private DdonGameServer Server { get; } | ||
|
||
public event EventHandler<AssetChangedEventArgs> AssetChanged; | ||
private readonly DirectoryInfo ScriptsDirectory; | ||
private readonly Dictionary<string, FileSystemWatcher> FileSystemWatchers; | ||
|
||
public ScriptManager(DdonGameServer server) | ||
{ | ||
Server = server; | ||
|
||
ScriptsRoot = $"{server.AssetRepository.AssetsPath}\\scripts"; | ||
SettingsPath = $"{ScriptsRoot}\\Settings.csx"; | ||
ExtendedFacilitiesPath = $"{ScriptsRoot}\\extended_facilities"; | ||
|
||
NpcExtendedFacilities = new Dictionary<NpcId, INpcExtendedFacility>(); | ||
|
||
Globals = new Globals() | ||
{ | ||
Server = server, | ||
GameLogicSetting = server.Setting.GameLogicSetting | ||
}; | ||
|
||
ScriptsDirectory = new DirectoryInfo(ScriptsRoot); | ||
if (!ScriptsDirectory.Exists) | ||
{ | ||
Logger.Error($"Could not compile scripts, unable to locate the path '{ScriptsRoot}'"); | ||
return; | ||
} | ||
|
||
FileSystemWatchers = new Dictionary<string, FileSystemWatcher>(); | ||
|
||
CompiledScripts = new Dictionary<string, Script>(); | ||
|
||
CompileScripts(); | ||
} | ||
|
||
private void CompileScripts() | ||
{ | ||
var options = ScriptOptions.Default | ||
.AddReferences(MetadataReference.CreateFromFile(typeof(DdonGameServer).Assembly.Location)) | ||
.AddReferences(MetadataReference.CreateFromFile(typeof(GameLogicSetting).Assembly.Location)) | ||
.AddReferences(MetadataReference.CreateFromFile(typeof(ScriptManager).Assembly.Location)) | ||
.AddReferences(MetadataReference.CreateFromFile(typeof(WalletType).Assembly.Location)) | ||
.AddImports("System", "System.Collections", "System.Collections.Generic") | ||
.AddImports("Arrowgene.Ddon.Shared") | ||
.AddImports("Arrowgene.Ddon.Shared.Model") | ||
.AddImports("Arrowgene.Ddon.GameServer") | ||
.AddImports("Arrowgene.Ddon.GameServer.Scripting") | ||
.AddImports("Arrowgene.Ddon.Shared.Entity.PacketStructure"); | ||
|
||
// Load The Game Settings | ||
Settings = CSharpScript.Create( | ||
code: File.ReadAllText(SettingsPath), | ||
options: options, | ||
globalsType: typeof(Globals) | ||
); | ||
// Execute the script file to populate the settings | ||
Settings.RunAsync(Globals); | ||
|
||
foreach (var file in Directory.EnumerateFiles(ExtendedFacilitiesPath)) | ||
{ | ||
var script = CSharpScript.Create( | ||
code: File.ReadAllText(file), | ||
options: options, | ||
globalsType: typeof(Globals) | ||
); | ||
|
||
var result = script.RunAsync(Globals).Result; | ||
if (result == null) | ||
{ | ||
Logger.Error($"Failed to parse {file}. Skipping."); | ||
continue; | ||
} | ||
|
||
INpcExtendedFacility extendedFacility = (INpcExtendedFacility)result.ReturnValue; | ||
NpcExtendedFacilities[extendedFacility.NpcId] = extendedFacility; | ||
} | ||
|
||
// TODO: Load other game functionality we want to have scripted. | ||
} | ||
} | ||
} |
Oops, something went wrong.