diff --git a/applications/Decoder/Decoder.cs b/applications/Decoder/Decoder.cs index 51ea4dd2..ff2c33c3 100644 --- a/applications/Decoder/Decoder.cs +++ b/applications/Decoder/Decoder.cs @@ -39,7 +39,7 @@ class Decoder { private static int BUFSIZE = 8096; private static SecondLife client = new SecondLife(); - private static ProtocolManager protocol = new ProtocolManager("keywords.txt", "message_template.msg", client); + private static ProtocolManager protocol = new ProtocolManager("message_template.msg", client); private static string grep = null; private static byte[] data = new byte[BUFSIZE]; private static byte[] temp = new byte[BUFSIZE]; diff --git a/applications/Template2Keywords/Template2Keywords.cs b/applications/Template2Keywords/Template2Keywords.cs index 4dbeccdb..bae53767 100644 --- a/applications/Template2Keywords/Template2Keywords.cs +++ b/applications/Template2Keywords/Template2Keywords.cs @@ -65,14 +65,17 @@ namespace Template2Keywords b *= 2; b &= 0x1FFF; + uint start = b; + while (table.ContainsKey(b)) { if (table[b] == s) return b; b++; - if (b > 0x1FFF) - return 0; //Give up looking, went past the end. (Shouldn't happen) + b &= 0x1FFF; + if (b == start) + return 0; //Give up looking, went through all values. (Shouldn't happen) } table[b] = s; diff --git a/bin/mapgen.bat b/bin/mapgen.bat index 5cf5f4bf..3a7e8a64 100644 --- a/bin/mapgen.bat +++ b/bin/mapgen.bat @@ -1 +1 @@ -mapgenerator.exe ../data/keywords.txt ../data/message_template.msg ../libsecondlife-cs/mapgenerator/template.cs ../libsecondlife-cs/_Packets_.cs \ No newline at end of file +mapgenerator.exe ../data/message_template.msg ../libsecondlife-cs/mapgenerator/template.cs ../libsecondlife-cs/_Packets_.cs \ No newline at end of file diff --git a/libsecondlife-cs/ProtocolManager.cs b/libsecondlife-cs/ProtocolManager.cs index c04e8ddc..667f0e76 100644 --- a/libsecondlife-cs/ProtocolManager.cs +++ b/libsecondlife-cs/ProtocolManager.cs @@ -184,7 +184,6 @@ namespace libsecondlife public MapPacket[] HighMaps; private SecondLife Client; - private int i = 0; /// /// @@ -192,7 +191,7 @@ namespace libsecondlife /// /// /// - public ProtocolManager(string keywordFile, string mapFile, SecondLife client) + public ProtocolManager(string mapFile, SecondLife client) { Client = client; @@ -223,7 +222,7 @@ namespace libsecondlife TypeSizes.Add(FieldType.Variable, -1); TypeSizes.Add(FieldType.Fixed, -2); - LoadKeywordFile(keywordFile); + KeywordPositions = new Dictionary(); LoadMapFile(mapFile); } @@ -375,36 +374,6 @@ namespace libsecondlife } } - /// - /// - /// - /// - private void LoadKeywordFile(string keywordFile) - { - string line; - StreamReader file; - - KeywordPositions = new Dictionary(); - - // Load the keyword file - try - { - file = File.OpenText(keywordFile); - } - catch(Exception e) - { - Client.Log("Error opening \"" + keywordFile + "\": " + e.Message, Helpers.LogLevel.Error); - throw new Exception("Keyword file error", e); - } - - while((line = file.ReadLine()) != null) - { - KeywordPositions.Add(line.Trim(), i++); - } - - file.Close(); - } - /// /// /// @@ -530,6 +499,9 @@ namespace libsecondlife if (tokens.Length > 3) { + //Hash packet name to insure correct keyword ordering + KeywordPosition(tokens[0]); + if (tokens[1] == "Fixed") { // Remove the leading "0x" @@ -688,15 +660,34 @@ namespace libsecondlife private int KeywordPosition(string keyword) { - if (KeywordPositions.ContainsKey(keyword)) - { - return (int)KeywordPositions[keyword]; - } - else - { - Client.Log("Couldn't find keyword: " + keyword, Helpers.LogLevel.Warning); - return -1; - } + if (KeywordPositions.ContainsKey(keyword)) + { + return KeywordPositions[keyword]; + } + + int hash = 0; + for (int i = 1; i < keyword.Length; i++) + { + hash = (hash + (int)(keyword[i])) * 2; + } + hash *= 2; + hash &= 0x1FFF; + + int startHash = hash; + + while (KeywordPositions.ContainsValue(hash)) + { + hash++; + hash &= 0x1FFF; + if (hash == startHash) + { + //Give up looking, went through all values and they were all taken. + throw new Exception("All hash values are taken. Failed to add keyword: " + keyword); + } + } + + KeywordPositions[keyword] = hash; + return hash; } } } diff --git a/libsecondlife-cs/examples/sldump/sldump.cs b/libsecondlife-cs/examples/sldump/sldump.cs index 9473c2bd..1132939c 100644 --- a/libsecondlife-cs/examples/sldump/sldump.cs +++ b/libsecondlife-cs/examples/sldump/sldump.cs @@ -88,7 +88,7 @@ namespace sldump try { - protocol = new ProtocolManager("keywords.txt", "message_template.msg", client); + protocol = new ProtocolManager("message_template.msg", client); } catch (Exception e) { diff --git a/libsecondlife-cs/mapgenerator/ProtocolManager.cs b/libsecondlife-cs/mapgenerator/ProtocolManager.cs index 1bec2d9c..197a6fbc 100644 --- a/libsecondlife-cs/mapgenerator/ProtocolManager.cs +++ b/libsecondlife-cs/mapgenerator/ProtocolManager.cs @@ -183,15 +183,13 @@ namespace libsecondlife /// public MapPacket[] HighMaps; - private int i = 0; - /// /// /// /// /// /// - public ProtocolManager(string keywordFile, string mapFile) + public ProtocolManager(string mapFile) { // Initialize the map arrays LowMaps = new MapPacket[65536]; @@ -220,7 +218,7 @@ namespace libsecondlife TypeSizes.Add(FieldType.Variable, -1); TypeSizes.Add(FieldType.Fixed, -2); - LoadKeywordFile(keywordFile); + KeywordPositions = new Dictionary(); LoadMapFile(mapFile); } @@ -372,36 +370,6 @@ namespace libsecondlife } } - /// - /// - /// - /// - private void LoadKeywordFile(string keywordFile) - { - string line; - StreamReader file; - - KeywordPositions = new Dictionary(); - - // Load the keyword file - try - { - file = File.OpenText(keywordFile); - } - catch(Exception e) - { - //Client.Log("Error opening \"" + keywordFile + "\": " + e.Message, Helpers.LogLevel.Error); - throw new Exception("Keyword file error", e); - } - - while((line = file.ReadLine()) != null) - { - KeywordPositions.Add(line.Trim(), i++); - } - - file.Close(); - } - /// /// /// @@ -527,6 +495,9 @@ namespace libsecondlife if (tokens.Length > 3) { + //Hash packet name to insure correct keyword ordering + KeywordPosition(tokens[0]); + if (tokens[1] == "Fixed") { // Remove the leading "0x" @@ -689,13 +660,32 @@ namespace libsecondlife { if (KeywordPositions.ContainsKey(keyword)) { - return (int)KeywordPositions[keyword]; - } - else - { - //Client.Log("Couldn't find keyword: " + keyword, Helpers.LogLevel.Warning); - throw new Exception("Couldn't find keyword " + keyword); + return KeywordPositions[keyword]; } + + int hash = 0; + for (int i = 1; i < keyword.Length; i++) + { + hash = (hash + (int)(keyword[i])) * 2; + } + hash *= 2; + hash &= 0x1FFF; + + int startHash = hash; + + while (KeywordPositions.ContainsValue(hash)) + { + hash++; + hash &= 0x1FFF; + if (hash == startHash) + { + //Give up looking, went through all values and they were all taken. + throw new Exception("All hash values are taken. Failed to add keyword: " + keyword); + } + } + + KeywordPositions[keyword] = hash; + return hash; } - } + } } diff --git a/libsecondlife-cs/mapgenerator/mapgenerator.cs b/libsecondlife-cs/mapgenerator/mapgenerator.cs index c26e13cf..217c9952 100644 --- a/libsecondlife-cs/mapgenerator/mapgenerator.cs +++ b/libsecondlife-cs/mapgenerator/mapgenerator.cs @@ -707,16 +707,16 @@ namespace mapgenerator try { - if (args.Length < 4) + if (args.Length < 3) { - Console.WriteLine("Invalid arguments, need [keywords.txt] [message_template.msg] [template.cs] [_Packets_.cs]"); + Console.WriteLine("Invalid arguments, need [message_template.msg] [template.cs] [_Packets_.cs]"); return -1; } - writer = new StreamWriter(args[3]); - protocol = new ProtocolManager(args[0], args[1]); + writer = new StreamWriter(args[2]); + protocol = new ProtocolManager(args[0]); - TextReader reader = new StreamReader(args[2]); + TextReader reader = new StreamReader(args[1]); writer.WriteLine(reader.ReadToEnd()); reader.Close(); }