Skip to content

Commit 316e09b

Browse files
committed
shortguid table linking now done automatically
1 parent 3d97178 commit 316e09b

2 files changed

Lines changed: 65 additions & 35 deletions

File tree

CathodeLib/Scripts/CATHODE/Commands/Helpers/CommandsUtils.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public CommandsUtils(Commands commands)
4545
LoadInfo(_commands.Filepath);
4646

4747
_nameID = ShortGuidUtils.Generate("name").AsUInt32;
48+
49+
ShortGuidUtils.LinkCommands(_commands);
50+
}
51+
52+
~CommandsUtils()
53+
{
54+
ShortGuidUtils.UnlinkCommands(_commands);
4855
}
4956

5057
#region Generic Utility Functions
@@ -1284,20 +1291,28 @@ public ParameterVariant PinTypeToParameterVariant(CompositePinType type)
12841291
private void LoadInfo(string filepath)
12851292
{
12861293
_compPurges = (CompositePurgeTable)CustomTable.ReadTable(filepath, CustomTableType.COMPOSITE_PURGE_STATES);
1287-
if (_compPurges == null) _compPurges = new CompositePurgeTable();
1288-
Console.WriteLine("Registered " + _compPurges.purged.Count + " pre-purged composites!");
1294+
if (_compPurges == null)
1295+
_compPurges = new CompositePurgeTable();
1296+
else
1297+
Console.WriteLine("Registered " + _compPurges.purged.Count + " pre-purged composites!");
12891298

12901299
_entityNames = (EntityNameTable)CustomTable.ReadTable(filepath, CustomTableType.ENTITY_NAMES);
1291-
if (_entityNames == null) _entityNames = new EntityNameTable();
1292-
Console.WriteLine("Loaded " + _entityNames.names.Count + " custom entity names!");
1300+
if (_entityNames == null)
1301+
_entityNames = new EntityNameTable();
1302+
else
1303+
Console.WriteLine("Loaded " + _entityNames.names.Count + " custom entity names!");
12931304

12941305
_modificationInfo = (CompositeModificationInfoTable)CustomTable.ReadTable(filepath, CustomTableType.COMPOSITE_MODIFICATION_INFO);
1295-
if (_modificationInfo == null) _modificationInfo = new CompositeModificationInfoTable();
1296-
Console.WriteLine("Loaded modification info for " + _modificationInfo.modification_info.Count + " composites!");
1306+
if (_modificationInfo == null)
1307+
_modificationInfo = new CompositeModificationInfoTable();
1308+
else
1309+
Console.WriteLine("Loaded modification info for " + _modificationInfo.modification_info.Count + " composites!");
12971310

12981311
_pinInfo = (CompositePinInfoTable)CustomTable.ReadTable(filepath, CustomTableType.COMPOSITE_PIN_INFO);
1299-
if (_pinInfo == null) _pinInfo = new CompositePinInfoTable();
1300-
Console.WriteLine("Loaded custom pin info for " + _pinInfo.composite_pin_infos.Count + " composites!");
1312+
if (_pinInfo == null)
1313+
_pinInfo = new CompositePinInfoTable();
1314+
else
1315+
Console.WriteLine("Loaded custom pin info for " + _pinInfo.composite_pin_infos.Count + " composites!");
13011316
}
13021317
private void SaveInfo(string filepath)
13031318
{

CathodeLib/Scripts/CATHODE/Commands/Helpers/ShortGuidUtils.cs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,21 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.IO;
7+
using System.Linq;
8+
using System.Runtime.CompilerServices;
79
using System.Security.Cryptography;
810
using System.Text;
911
#if UNITY_EDITOR || UNITY_STANDALONE
1012
using UnityEngine;
1113
#endif
1214

15+
[assembly: InternalsVisibleTo("CATHODE.Scripting")]
1316
namespace CATHODE.Scripting
1417
{
1518
public static class ShortGuidUtils
1619
{
1720
private static GuidNameTable _custom = new GuidNameTable();
1821

19-
public static Commands LinkedCommands => _commands;
20-
private static Commands _commands;
21-
22-
/* Optionally, link a Commands file which can be used to save custom ShortGuids to */
23-
public static void LinkCommands(Commands commands)
24-
{
25-
if (_commands != null)
26-
_commands.OnSaveSuccess -= SaveCustomNames;
27-
28-
_commands = commands;
29-
if (_commands == null) return;
30-
31-
_commands.OnSaveSuccess += SaveCustomNames;
32-
33-
LoadCustomNames(commands.Filepath);
34-
}
35-
3622
/* Generate a ShortGuid to interface with the Cathode scripting system */
3723
public static ShortGuid Generate(string value, bool cache = true)
3824
{
@@ -86,31 +72,60 @@ public static ShortGuid GenerateRandom()
8672
}
8773

8874
/* Cache a pre-generated ShortGuid */
89-
private static void Cache(ShortGuid guid, string value)
75+
private static bool Cache(ShortGuid guid, string value)
9076
{
91-
//TODO: need to fix this for BSPNOSTROMO_RIPLEY_PATCH (?)
92-
if (_custom.cache.ContainsKey(value)) return;
77+
if (_custom.cache.ContainsKey(value)) return false;
9378
_custom.cache.Add(value, guid);
9479
try
9580
{
81+
//TODO: need to fix this for BSPNOSTROMO_RIPLEY_PATCH (?)
9682
_custom.cacheReversed.Add(guid, value);
9783
}
9884
catch { }
85+
return true;
9986
}
10087

101-
/* Pull non-vanilla ShortGuid from the CommandsPAK */
102-
private static void LoadCustomNames(string filepath)
88+
#region Commands Linking
89+
private static List<Commands> _commands = new List<Commands>();
90+
91+
/* Utilised by CommandsUtils: this connects up a loaded Commands object, allowing generated ShortGuids to be stored for re-use next session */
92+
internal static void LinkCommands(Commands commands)
10393
{
104-
_custom = (GuidNameTable)CustomTable.ReadTable(filepath, CustomTableType.SHORT_GUIDS);
105-
if (_custom == null) _custom = new GuidNameTable();
106-
Console.WriteLine("Loaded " + _custom.cache.Count + " custom ShortGuids!");
94+
if (_commands.FirstOrDefault(o => o.Filepath == commands.Filepath) != null)
95+
return;
96+
97+
_commands.Add(commands);
98+
commands.OnSaveSuccess += SaveCustomNames;
99+
LoadCustomNames(commands.Filepath);
107100
}
101+
internal static void UnlinkCommands(Commands commands)
102+
{
103+
Commands linkedCommands = _commands.FirstOrDefault(o => o.Filepath == commands.Filepath);
104+
if (linkedCommands == null)
105+
return;
106+
_commands.Remove(linkedCommands);
107+
}
108+
109+
/* Load/save custom shortguids */
110+
private static void LoadCustomNames(string filepath)
111+
{
112+
GuidNameTable guids = (GuidNameTable)CustomTable.ReadTable(filepath, CustomTableType.SHORT_GUIDS);
113+
if (guids == null)
114+
return;
108115

109-
/* Write non-vanilla entity names to the CommandsPAK */
116+
int added = 0;
117+
foreach (KeyValuePair<string, ShortGuid> str in guids.cache)
118+
{
119+
if (Cache(str.Value, str.Key))
120+
added++;
121+
}
122+
Console.WriteLine("Loaded " + added + " ShortGuids!");
123+
}
110124
private static void SaveCustomNames(string filepath)
111125
{
112126
CustomTable.WriteTable(filepath, CustomTableType.SHORT_GUIDS, _custom);
113-
Console.WriteLine("Saved " + _custom.cache.Count + " custom ShortGuids!");
127+
Console.WriteLine("Saved " + _custom.cache.Count + " ShortGuids!");
114128
}
129+
#endregion
115130
}
116131
}

0 commit comments

Comments
 (0)