예제 #1
0
        private async Task <object> GetSymbolValue(EScriptToken type)
        {
            switch (type)
            {
            case EScriptToken.ESCRIPTTOKEN_FLOAT:
                return(_bs.ReadSingle());

            case EScriptToken.ESCRIPTTOKEN_INTEGER:
                return(_bs.ReadInt32());

            case EScriptToken.ESCRIPTTOKEN_NAME:
                var checksum = _bs.ReadUInt32();
                try
                {
                    return(await _resolver.ResolveChecksum(checksum));
                } catch
                {
                    return(checksum);
                }

            case EScriptToken.ESCRIPTTOKEN_LOCALSTRING:
            case EScriptToken.ESCRIPTTOKEN_STRING:

                var    length = _bs.ReadInt32();
                string s      = "";
                while (length-- > 0)
                {
                    var c = _bs.ReadChar();
                    if (c != 0)
                    {
                        s += c;
                    }
                }
                return(s);

            case EScriptToken.ESCRIPTTOKEN_KEYWORD_SCRIPT:
                _bs.ReadByte();          //function size
                return(_bs.ReadInt32()); //script name

            //case EScriptToken.ESCRIPTTOKEN_KEYWORD_ENDSCRIPT:
            case EScriptToken.ESCRIPTTOKEN_COMMA:
            case EScriptToken.ESCRIPTTOKEN_ENDOFLINE:
            case EScriptToken.ESCRIPTTOKEN_EQUALS:
            case EScriptToken.ESCRIPTTOKEN_STARTSTRUCT:
            case EScriptToken.ESCRIPTTOKEN_ENDSTRUCT:
            case EScriptToken.ESCRIPTTOKEN_STARTARRAY:
            case EScriptToken.ESCRIPTTOKEN_ENDARRAY:
            case EScriptToken.ESCRIPTTOKEN_OPENPARENTH:
            case EScriptToken.ESCRIPTTOKEN_CLOSEPARENTH:
            case EScriptToken.ESCRIPTTOKEN_ARG:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_ALLARGS:
            case EScriptToken.ESCRIPTTOKEN_GREATERTHAN:
            case EScriptToken.ESCRIPTTOKEN_GREATERTHANEQUAL:
            case EScriptToken.ESCRIPTTOKEN_LESSTHAN:
            case EScriptToken.ESCRIPTTOKEN_LESSTHANEQUAL:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_ENDIF:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_ENDSCRIPT:
            case EScriptToken.ESCRIPTTOKEN_MULTIPLY:
            case EScriptToken.ESCRIPTTOKEN_DIVIDE:
            case EScriptToken.ESCRIPTTOKEN_DOT:
            case EScriptToken.ESCRIPTTOKEN_ADD:
            case EScriptToken.ESCRIPTTOKEN_MINUS:
            case EScriptToken.ESCRIPTTOKEN_OR:
            case EScriptToken.ESCRIPTTOKEN_AND:
            case EScriptToken.ESCRIPTTOKEN_COLON:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_SWITCH:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_ENDSWITCH:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_NOT:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_RANDOM:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_BEGIN:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_REPEAT:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_CASE:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_DEFAULT:
            case EScriptToken.ESCRIPTTOKEN_KEYWORD_RETURN:
                return(null);

            case EScriptToken.ESCRIPTTOKEN_RUNTIME_RELATIVE_JUMP:
                return(_bs.ReadInt16());

            case EScriptToken.ESCRIPTTOKEN_RUNTIME_IF2:
            case EScriptToken.ESCRIPTTOKEN_RUNTIME_ELSE2:
                return(_bs.ReadInt16());    //jump length

            case EScriptToken.ESCRIPTTOKEN_ENDOFLINENUMBER:
                return(null);

            case EScriptToken.ESCRIPTTOKEN_CHECKSUM_NAME:
                var    checksum_number = _bs.ReadUInt32();
                string checksum_name   = "";
                while (true)
                {
                    var c = _bs.ReadChar();
                    if (c == 0)
                    {
                        break;
                    }
                    checksum_name += c;
                }
                if (insertChecksums)
                {
                    var generated_checksum = await _resolver.GenerateChecksum(checksum_name);

                    if (generated_checksum != checksum_number)
                    {
                        throw new ArgumentException("Checksum mismatch", checksum_name);
                    }
                }

                return(checksum_name);

            case EScriptToken.ESCRIPTTOKEN_VECTOR:
                var res = new float[3];
                res[0] = _bs.ReadSingle();
                res[1] = _bs.ReadSingle();
                res[2] = _bs.ReadSingle();
                return(res);

            default:
                throw new ArgumentException("Got unhandled symbol", type.ToString());
            }
        }
        public async Task <MemoryStream> SerializeCAS(CASData saveData)
        {
            var qw = new SymbolBufferWriter();

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(ms))
                {
                    FileHeader header = new FileHeader();
                    header.Write(bw); //write place holder

                    int header_size = (int)ms.Position;

                    var retMs = new MemoryStream();


                    var summary_info = (List <SymbolEntry>)saveData.summary;
                    summary_info = await GenerateChecksums(summary_info);

                    qw.SerializeBuffer(bw, summary_info);
                    var summary_size = ms.Position - header_size;

                    var save_data = (List <SymbolEntry>)saveData.save_data;
                    save_data = await GenerateChecksums(save_data);

                    qw.SerializeBuffer(bw, save_data);
                    var save_size = ms.Position - summary_size - header_size;

                    byte[] summary_buffer = summary_buffer = ms.GetBuffer().Skip(header_size).Take((int)summary_size).ToArray();

                    header.summaryInfoChecksum = await checksumResolver.GenerateChecksum(summary_buffer);

                    header.checksum        = 0;
                    header.dataSize        = (int)(summary_size + header_size + save_size);
                    header.summaryInfoSize = (int)summary_size;
                    header.version         = version;

                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    header.Write(bw);

                    byte[] buff = ms.GetBuffer().Take(header.dataSize).ToArray();
                    header.checksum = await checksumResolver.GenerateChecksum(buff);

                    bw.BaseStream.Seek(0, SeekOrigin.Begin);
                    header.Write(bw);
                    buff = ms.GetBuffer();

                    bw.BaseStream.Seek(0, SeekOrigin.End);
                    var desired_size = fixedFileSize - header.dataSize;
                    for (int i = 0; i < desired_size; i++)
                    {
                        bw.Write((byte)'i');
                    }

                    ms.WriteTo(retMs);



                    return(retMs);
                }
            }
        }