Ejemplo n.º 1
0
        public static void LoadTbl(string path)
        {
            int          count = 0;
            StreamReader sr    = new StreamReader(path, Encoding.UTF8);

            while (sr.Peek() >= 0)
            {
                string line = sr.ReadLine();
                int    pos  = line.IndexOf("=");
                if (pos < 0)
                {
                    continue;
                }
                string hex  = line.Substring(0, pos);
                string word = line.Substring(pos + 1, line.Length - (pos + 1));
                if (!dict.ContainsKey(word))
                {
                    dict.Add(word, ScriptUtil.Hex2Byte(hex));
                    count++;
                }
                else
                {
                    Console.WriteLine("重复的汉字!跳过。{0}", line);
                }
            }

            sr.Close();
            Console.WriteLine("已加载自定义编码表,有效字符数:{0}", count);
        }
Ejemplo n.º 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                if (objectType == null)
                {
                    throw new Exception(string.Format("不能转换null value to {0}.", objectType));
                }
                return(null);
            }

            if (reader.TokenType == JsonToken.String)
            {
                string hex = (string)reader.Value;
                if (hex.Length >= 4 && hex.Substring(0, 2).Equals("0x", StringComparison.OrdinalIgnoreCase))
                {
                    return(ScriptUtil.Hex2Byte(hex));
                }
            }

            return(reader.Value);
        }
Ejemplo n.º 3
0
        private byte[] CompileCode(string code, long position)
        {
            code += " ";
            MemoryStream ms  = new MemoryStream();
            BinaryWriter mbw = new BinaryWriter(ms);

            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            int i = 0;

            while (i + 4 <= code.Length && code.Substring(i, 4) == "    ")
            {
                i += 4;
            }
            int len_pos = (int)ms.Position;

            mbw.Write(new byte[2]);//长度填充

            string token = "";
            bool   str   = false;

            for (; i < code.Length; i++)
            {
                if (code[i] == '\"')
                {
                    str = !str;
                }
                if (code[i] == ' ' && !str)
                {
                    switch (token[0])
                    {
                    case '<':
                        int goto_line = Convert.ToInt32(token.Substring(1, token.Length - 2));
                        goto_position.Add((uint)(position + ms.Position), goto_line);
                        mbw.Write(BitConverter.GetBytes((uint)0));
                        break;

                    case '[':    //byte
                        mbw.Write(ScriptUtil.Hex2Byte(token.Substring(1, token.Length - 2)));
                        break;

                    case '(':    //uint16
                        mbw.Write(BitConverter.GetBytes(Convert.ToUInt16(token.Substring(1, token.Length - 2))));
                        break;

                    case '{':    //uint32
                        mbw.Write(BitConverter.GetBytes(Convert.ToUInt32(token.Substring(1, token.Length - 2))));
                        break;

                    case '$':
                    {
                        token = token.Replace(@"\n", "\n");
                        token = token.Replace(@"{\n}", "\n"); // 兼容保留
                        string tmp = token.Substring(3, token.Length - 4);
                        if (token[1] == 'u')                  //unicode string + 00 00
                        {
                            mbw.Write(Encoding.Unicode.GetBytes(tmp));
                            mbw.Write(new byte[] { 0x00, 0x00 });
                        }
                        else if (token[1] == 'j')        //sjis string + 00
                        {
                            mbw.Write(Encoding.GetEncoding("Shift-Jis").GetBytes(tmp));
                            mbw.Write((byte)0x00);
                        }
                        else if (token[1] == '8')        //utf8 string + 00
                        {
                            mbw.Write(Encoding.UTF8.GetBytes(tmp));
                            mbw.Write((byte)0x00);
                        }
                        else if (token[1] == 'c')        //custom string + 00
                        {
                            mbw.Write(CustomEncoding.GetBytes(tmp));
                            mbw.Write((byte)0x00);
                        }
                        break;
                    }

                    case '&':
                    {
                        token = token.Replace(@"\n", "\n");
                        token = token.Replace(@"{\n}", "\n"); // 兼容保留
                        string tmp = token.Substring(3, token.Length - 4);
                        if (token[1] == 'u')                  //len + unicode string
                        {
                            mbw.Write(BitConverter.GetBytes((UInt16)tmp.Length));
                            mbw.Write(Encoding.Unicode.GetBytes(tmp));
                        }
                        else if (token[1] == 'j')        //len + sjis string
                        {
                            mbw.Write(BitConverter.GetBytes((UInt16)tmp.Length));
                            mbw.Write(Encoding.GetEncoding("Shift-Jis").GetBytes(tmp));
                        }
                        else if (token[1] == 'c')        //custom string + 00
                        {
                            mbw.Write(BitConverter.GetBytes((UInt16)tmp.Length));
                            mbw.Write(CustomEncoding.GetBytes(tmp));
                        }
                        break;
                    }

                    case 'u':     // 兼容保留
                        token = token.Replace(@"{\n}", "\n");
                        mbw.Write(Encoding.Unicode.GetBytes(token.Substring(2, token.Length - 3)));
                        mbw.Write(new byte[] { 0x00, 0x00 });
                        break;

                    case 'j':     // 兼容保留
                        token = token.Replace(@"{\n}", "\n");
                        mbw.Write(Encoding.GetEncoding("Shift-Jis").GetBytes(token.Substring(2, token.Length - 3)));
                        mbw.Write((byte)0x00);
                        break;

                    case 'p':     // 兼容保留
                        token = token.Replace(@"{\n}", "\n");
                        mbw.Write(BitConverter.GetBytes((UInt16)(token.Length - 3)));
                        mbw.Write(Encoding.Unicode.GetBytes(token.Substring(2, token.Length - 3)));
                        break;

                    case 's':     // 兼容保留
                        token = token.Replace(@"{\n}", "\n");
                        mbw.Write(BitConverter.GetBytes((UInt16)(token.Length - 3)));
                        mbw.Write(Encoding.GetEncoding("Shift-Jis").GetBytes(token.Substring(2, token.Length - 3)));
                        break;

                    default:
                        if (token[0] >= 'A' && token[0] <= 'Z')
                        {
                            int    index_info = token.IndexOf('(');
                            string info       = "";
                            if (index_info > 0)
                            {
                                info  = token.Substring(index_info);
                                token = token.Substring(0, index_info);
                            }
                            if (compress_dic.ContainsKey(token))
                            {
                                if (ScriptVersion == 2)
                                {
                                    // [opcode] length/2 code
                                    // 1byte  1byte
                                    ms.Seek(len_pos, SeekOrigin.Begin);
                                    mbw.Write(compress_dic[token]);
                                    mbw.Write(new byte[1]);
                                }
                                else if (ScriptVersion == 2)
                                {
                                    // length [opcode] code
                                    // 2byte  1byte
                                    mbw.Write(compress_dic[token]);
                                }
                            }
                            if (index_info > 0)
                            {
                                if (info.Length > 2)
                                {
                                    string[] info_split = info.Substring(1, info.Length - 2).Split(',');
                                    if (ScriptVersion == 3)
                                    {
                                        mbw.Write((byte)info_split.Length);
                                    }
                                    foreach (string str1 in info_split)
                                    {
                                        mbw.Write(BitConverter.GetBytes(Convert.ToUInt16(str1)));
                                    }
                                }
                                else
                                {
                                    mbw.Write((byte)0);
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("未知的指令或错误的数据格式:" + token);
                        }
                        break;
                    }
                    token = "";
                }
                else
                {
                    token += code[i];
                }
            }
            UInt16 len = (UInt16)(ms.Position - len_pos);

            if (ScriptVersion == 2)
            {
                // opcode [length/2] code
                // 1byte  1byte
                ms.Seek(len_pos + 1, SeekOrigin.Begin);
                mbw.Write((byte)(len / 2));
            }
            else if (ScriptVersion == 3)
            {
                // [length] opcode code
                // 2byte  1byte
                if (len % 2 != 0)
                {
                    mbw.Write((byte)0x00);
                }
                ms.Seek(len_pos, SeekOrigin.Begin);
                mbw.Write(BitConverter.GetBytes(len));
            }

            byte[] retn = ms.ToArray();
            if (retn.Length > 0 && Program.debug)
            {
                Console.WriteLine(ScriptUtil.Byte2Hex(retn, true));
            }
            mbw.Close();
            ms.Close();
            return(retn);
        }