Exemplo n.º 1
0
    // void ReadActions()
    // {
    //     // Read actions from nwscript.nss
    //     actions = new Dictionary<int, MethodInfo>();
    //     Stream stream = AuroraData.Instance.GetStream("nwscript.nss", AuroraEngine.ResourceType.NSS);
    //     string text = new StreamReader(stream).ReadToEnd();
    //     UnityEngine.Debug.Log("Loaded nwscript.nss: " + text);
    // }

    // public NCSScript(Stream stream, string name)
    // {
    //     // Firstly, we save the script to disk

    //     byte[] buffer = new byte[stream.Length];
    //     stream.Read(buffer, 0, (int)stream.Length);

    //     FileStream filestream = File.Create("D:\\KOTOR\\KotOR-Unity\\tmp\\tmp.ncs");
    //     stream.Seek(0, SeekOrigin.Begin);
    //     stream.CopyTo(filestream);
    //     filestream.Close();

    //     // Then we read it using xoreos-tools's disassembler
    //     Process p = new Process();
    //     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    //     p.StartInfo.CreateNoWindow = true;
    //     p.StartInfo.UseShellExecute = false;
    //     p.StartInfo.FileName = "D:\\KOTOR\\KOTOR1\\KotOR-Unity\\xt\\ncsdis.exe";
    //     p.StartInfo.Arguments = "--assembly --kotor D:\\KOTOR\\KotOR-Unity\\tmp\\tmp.ncs";
    //     p.StartInfo.RedirectStandardOutput = true;
    //     p.StartInfo.RedirectStandardError = true;
    //     p.EnableRaisingEvents = true;

    //     StringBuilder outputBuilder = new StringBuilder();
    //     StringBuilder errorBuilder = new StringBuilder();

    //     using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
    //     using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
    //     {
    //         p.OutputDataReceived += (sender, e) =>
    //         {
    //             if (e.Data == null)
    //             {
    //                 outputWaitHandle.Set();
    //             }
    //             else
    //             {
    //                 outputBuilder.AppendLine(e.Data);
    //             }
    //         };
    //         p.ErrorDataReceived += (sender, e) =>
    //         {
    //             if (e.Data == null)
    //             {
    //                 errorWaitHandle.Set();
    //             }
    //             else
    //             {
    //                 errorBuilder.AppendLine(e.Data);
    //             }
    //         };

    //         p.Start();

    //         p.BeginOutputReadLine();
    //         p.BeginErrorReadLine();

    //         if (p.WaitForExit(1000) &&
    //             outputWaitHandle.WaitOne() &&
    //             errorWaitHandle.WaitOne())
    //         {
    //             // Process completed. Check process.ExitCode here.
    //         }
    //         else
    //         {
    //             // Timed out.
    //         }
    //     }

    //     string output = outputBuilder.ToString();
    //     string error = errorBuilder.ToString();

    //     //UnityEngine.Debug.Log(output);
    //     //UnityEngine.Debug.Log(error);

    //     // Finally, we run the normal NCSScript reader on that
    //     ParseString(output, name);
    // }

    // public NCSScript(string script, string name)
    // {
    //     ParseString(script, name);
    // }

    // void ParseString(string script, string name)
    // {
    //     this.scriptName = name;
    //     string[] lines = script.Split(new[] { '\n' }, StringSplitOptions.None);
    //     //UnityEngine.Debug.Log(lines.Length);
    //     foreach (string rawline in lines)
    //     {
    //         // Determine the line type
    //         string line = rawline.Trim().Split(new char[] { ';' })[0].Trim();
    //         //UnityEngine.Debug.Log(line);
    //         if (line.Length == 0)
    //         {
    //             // This was a blank line or comment, so ignore it
    //             //Debug.Log("Ignoring blank/comment line: " + rawline);
    //         }
    //         else if (line[line.Length - 1] == ':')
    //         {
    //             // This is a label
    //             //Debug.Log("Registering label: " + rawline);

    //             string labelName = String.Concat(line.Take(line.Length - 1));
    //             // If we have n instructions, this label points to instruction n+1 (at index n+1-1=n)
    //             labels[labelName] = instructions.Count;
    //         }
    //         else
    //         {
    //             // This is an instruction
    //             //Debug.Log("Registering instruction: " + rawline);

    //             // TODO: Make this work for lines that have strings with spaces in them
    //             string[] rawInstruction = line.Split(new char[] { ' ' });

    //             // Remove quotes surrounding strings
    //             for (int i = 0; i < rawInstruction.Length; i++)
    //             {
    //                 string s = rawInstruction[i];
    //                 if (s.Length < 2)
    //                 {
    //                     continue;
    //                 }
    //                 if (s[0] == '"' && s[s.Length - 1] == '"')
    //                 {
    //                     s = s.Substring(1, s.Length - 2);
    //                     rawInstruction[i] = s;
    //                 }
    //             }

    //             string instructionName = rawInstruction[0];

    //             Type instructionType = Type.GetType("NCSInstructions." + instructionName);
    //             if (instructionType == null)
    //             {
    //                 throw new Exception("Instruction type was null for " + instructionName);
    //             }
    //             NCSInstruction instruction = (NCSInstruction)Activator.CreateInstance(instructionType);
    //             //Debug.Log(instruction);

    //             instruction.Initialize(rawInstruction, this);
    //             instructions.Add(instruction);
    //         }
    //     }
    // }

    // public void Start(NCSContext context)
    // {
    //     context.Start(this);
    // }

    public void Run(NCSContext context)
    {
        int steps = 0;

        Stopwatch watch = new Stopwatch();

        watch.Start();
        while (!context.Finished())
        {
            if (watch.ElapsedMilliseconds > 500)
            {
                throw new Exception("Executing script " + scriptName + " took over 500 milliseconds; stopped on line " + context.GetPC());
            }

            Step(context);
            steps++;
        }
    }
Exemplo n.º 2
0
 // Update is called once per frame
 void Update()
 {
     for (int i = 0; i < Speed; i++)
     {
         if (running && !context.Finished())
         {
             Debug.Log(context.programCounter + ": " + script.instructions[context.programCounter]);
             try
             {
                 script.Step(context);
             } catch
             {
                 running = false;
                 break;
             }
         }
         else
         {
             running = false;
             break;
         }
     }
 }