public CodeWriter(CodeParser parser, SteamApiDefinition def) { Parser = parser; this.def = def; WorkoutTypes(); }
private static void AddMissing(SteamApiDefinition output) { var content = System.IO.File.ReadAllText("steam_api_missing.json"); var missing = Newtonsoft.Json.JsonConvert.DeserializeObject <SteamApiDefinition>(content); output.structs.AddRange(missing.structs); output.methods.AddRange(missing.methods); }
static void Main(string[] args) { var content = System.IO.File.ReadAllText("steam_sdk/steam_api.json"); var def = Newtonsoft.Json.JsonConvert.DeserializeObject <SteamApiDefinition>(content); Definitions = def; var generator = new CodeWriter(def); generator.ToFolder("../../../Facepunch.Steamworks/Generated/"); }
private static void AddMissing(SteamApiDefinition output) { var content = System.IO.File.ReadAllText("steam_api_missing.json"); var missing = Newtonsoft.Json.JsonConvert.DeserializeObject <SteamApiDefinition>(content); output.structs.AddRange(missing.structs); output.methods.AddRange(missing.methods); foreach (var s in output.structs) { if (s.Fields == null) { s.Fields = new SteamApiDefinition.StructDef.StructFields[0]; } } }
internal void ExtendDefinition(SteamApiDefinition def) { // // Get a list of CallbackIds // def.CallbackIds = new Dictionary <string, int>(); { var r = new Regex(@"enum { (k_i(?:.+)) = ([0-9]+) };"); var ma = r.Matches(Content); foreach (Match m in ma) { def.CallbackIds.Add(m.Groups[1].Value.Replace("k_i", "").Replace("Callbacks", ""), int.Parse(m.Groups[2].Value)); } } // // Associate callbackIds with structs // foreach (var t in def.structs) { // Standard style { var r = new Regex(@"struct " + t.Name + @"\n{ ?\n(?:.)+enum { k_iCallback = (?:(.+) \+ ([0-9]+)|(.+)) };", RegexOptions.Multiline | RegexOptions.IgnoreCase); var m = r.Match(Content); if (m.Success) { var kName = m.Groups[1].Value; var num = m.Groups[2].Value; if (string.IsNullOrEmpty(kName)) { kName = m.Groups[3].Value; num = "0"; } kName = kName.Replace("k_i", "CallbackIdentifiers.").Replace("Callbacks", ""); t.CallbackId = $"{kName} + {num}"; } } // New style { var r = new Regex(@"DEFINE_CALLBACK\( " + t.Name + @", (.+) \+ ([0-9]+) \)"); var m = r.Match(Content); if (m.Success) { var kName = m.Groups[1].Value; var num = m.Groups[2].Value; kName = kName.Replace("k_i", "CallbackIdentifiers.").Replace("Callbacks", ""); t.CallbackId = $"{kName} + {num}"; } } } // // Find defines // def.Defines = new Dictionary <string, string>(); { var r = new Regex(@"#define ([a-zA-Z_]+) ""(.+)"""); var ma = r.Matches(Content); foreach (Match m in ma) { def.Defines.Add(m.Groups[1].Value.Replace("Callbacks", ""), m.Groups[2].Value); } } // // Find CALL_RESULTs // { var r = new Regex(@"CALL_RESULT\( (.+) \)(?:.+)?\n(?:.+)virtual\s+SteamAPICall_t\s+(\w+)\("); var ma = r.Matches(Content); foreach (Match m in ma) { var s = def.structs.Single(x => x.Name == m.Groups[1].Value); s.IsCallResult = true; foreach (var t in def.methods.Where(x => x.Name == m.Groups[2].Value)) { t.CallResult = s.Name; } } } }
public CodeWriter(SteamApiDefinition def) { this.def = def; WorkoutTypes(); }
private static void AddExtras(SteamApiDefinition def) { def.structs.Add(new SteamApiDefinition.StructDef() { Name = "ItemInstalled_t", Fields = new SteamApiDefinition.StructDef.StructFields[] { new SteamApiDefinition.StructDef.StructFields() { Name = "m_unAppID", Type = "AppId_t" }, new SteamApiDefinition.StructDef.StructFields() { Name = "m_nPublishedFileId", Type = "PublishedFileId_t" } } }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_Init", ReturnType = "bool", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_RunCallbacks", ReturnType = "void", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamGameServer_RunCallbacks", ReturnType = "void", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_RegisterCallback", ReturnType = "void", NeedsSelfPointer = false, Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "pCallback", Type = "void *" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "callback", Type = "int" }, } }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_UnregisterCallback", ReturnType = "void", NeedsSelfPointer = false, Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "pCallback", Type = "void *" } } }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_RegisterCallResult", ReturnType = "void", NeedsSelfPointer = false, Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "pCallback", Type = "void *" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "callback", Type = "SteamAPICall_t" }, } }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_UnregisterCallResult", ReturnType = "void", NeedsSelfPointer = false, Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "pCallback", Type = "void *" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "callback", Type = "SteamAPICall_t" }, } }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamInternal_GameServer_Init", ReturnType = "bool", NeedsSelfPointer = false, Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "unIP", Type = "uint32" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "usPort", Type = "uint16" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "usGamePort", Type = "uint16" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "usQueryPort", Type = "uint16" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "eServerMode", Type = "int" }, new SteamApiDefinition.MethodDef.ParamType() { Name = "pchVersionString", Type = "const char *" } }, }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_Shutdown", ReturnType = "void", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamGameServer_Shutdown", ReturnType = "void", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_GetHSteamUser", ReturnType = "HSteamUser", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamAPI_GetHSteamPipe", ReturnType = "HSteamPipe", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamGameServer_GetHSteamUser", ReturnType = "HSteamUser", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamGameServer_GetHSteamPipe", ReturnType = "HSteamPipe", NeedsSelfPointer = false }); def.methods.Add(new SteamApiDefinition.MethodDef() { ClassName = "SteamApi", Name = "SteamInternal_CreateInterface", ReturnType = "void *", Params = new SteamApiDefinition.MethodDef.ParamType[] { new SteamApiDefinition.MethodDef.ParamType() { Name = "version", Type = "const char *" } }, NeedsSelfPointer = false }); }
internal void ExtendDefinition(SteamApiDefinition def) { // // Get a list of CallbackIds // def.CallbackIds = new Dictionary <string, int>(); //v1 { var r = new Regex(@"enum { (k_[i|I](?:.+)) = ([0-9]+) };"); var ma = r.Matches(Content); foreach (Match m in ma) { def.CallbackIds.Add(m.Groups[1].Value.Substring(3).Replace("Callbacks", ""), int.Parse(m.Groups[2].Value)); } } // // Associate callbackIds with structs // foreach (var t in def.structs) { if (!string.IsNullOrEmpty(t.CallbackId)) { continue; } // Standard style { var r = new Regex(@"struct " + t.Name + @"\n{ ?\n(?:.)+enum { k_iCallback = (?:(.+) \+ ([0-9]+)|(.+)) };", RegexOptions.Multiline | RegexOptions.IgnoreCase); var m = r.Match(Content); if (m.Success) { var kName = m.Groups[1].Value; var num = m.Groups[2].Value; if (string.IsNullOrEmpty(kName)) { kName = m.Groups[3].Value; num = "0"; } kName = "CallbackIdentifiers." + kName.Substring(3).Replace("Callbacks", ""); t.CallbackId = $"{kName} + {num}"; } } // New style { var r = new Regex(@"DEFINE_CALLBACK\( " + t.Name + @", (.+) \+ ([0-9]+) \)"); var m = r.Match(Content); if (m.Success) { var kName = m.Groups[1].Value; var num = m.Groups[2].Value; //kName = kName.Replace( "k_i", "CallbackIdentifiers." ).Replace( "Callbacks", "" ); kName = "CallbackIdentifiers." + kName.Substring(3).Replace("Callbacks", ""); t.CallbackId = $"{kName} + {num}"; } } // Even Newer Style { var r = new Regex(@"STEAM_CALLBACK_BEGIN\( " + t.Name + @", (.+) \+ ([0-9]+) \)"); var m = r.Match(Content); if (m.Success) { var kName = m.Groups[1].Value; var num = m.Groups[2].Value; //kName = kName.Replace( "k_i", "CallbackIdentifiers." ).Replace( "Callbacks", "" ); kName = "CallbackIdentifiers." + kName.Substring(3).Replace("Callbacks", ""); t.CallbackId = $"{kName} + {num}"; } } } // // Find defines // def.Defines = new Dictionary <string, string>(); { var r = new Regex(@"#define ([a-zA-Z_]+) ""(.+)"""); var ma = r.Matches(Content); foreach (Match m in ma) { def.Defines.Add(m.Groups[1].Value.Replace("Callbacks", ""), m.Groups[2].Value); } } // // Find CALL_RESULTs // { var r = new Regex(@"CALL_RESULT\( (.+) \)(?:.+)?\n(?:.+)virtual\s+SteamAPICall_t\s+(\w+)\("); var ma = r.Matches(Content); foreach (Match m in ma) { var s = def.structs.Single(x => x.Name == m.Groups[1].Value); s.IsCallResult = true; foreach (var t in def.methods.Where(x => x.Name == m.Groups[2].Value)) { if (!string.IsNullOrEmpty(t.CallResult)) { continue; } t.CallResult = s.Name; } } } // // Find missing structs // { var r = new Regex(@"struct ([a-zA-Z]+_t)"); var ma = r.Matches(Content); foreach (Match m in ma) { var s = def.structs.SingleOrDefault(x => x.Name == m.Groups[1].Value); if (s == null) { Console.WriteLine("Missing Struct: " + m.Groups[1].Value); } } //Console.ReadKey(); } // // Change all struct bool fields to bytes (they're technically bytes anyway, and it helps with marshalling) // { foreach (var s in def.structs) { foreach (var f in s.Fields) { if (f.Type == "bool") { f.Type = "byte"; } } } } }