Exemplo n.º 1
0
        public static T Instance()
        {
            if (instance == null)
            {
                instance = FindObjectOfType <T>();

                if (FindObjectsOfType <T>().Length > 1)
                {
                    ZDebug.Log("More than 1!");
                }
                return(instance);
            }

            if (instance == null)
            {
                string instanceName = typeof(T).Name;
                ZDebug.Log("Instance Name: " + instanceName);
                GameObject instanceGO = GameObject.Find(instanceName);

                if (instanceGO == null)
                {
                    instanceGO = new GameObject(instanceName);
                }
                instance = instanceGO.AddComponent <T>();
                DontDestroyOnLoad(instanceGO);  // 不会被释放
                ZDebug.Log("Add New Singleton " + instance.name + " in Game!");
            }
            else
            {
                ZDebug.Log("Already exist: " + instance.name);
            }

            return(instance);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load an operand, either a variable or a constant.
        /// </summary>
        /// <param name="aType"></param>
        /// <param name="aArg"></param>
        private void LoadOperand(byte aType, out ushort aArg)
        {
            if ((aType & 0x02) > 0) // Variable
            {
                _machine.Memory.CodeByte(out byte xVariable);

                if (xVariable == 0)
                {
                    aArg = _machine.Memory.Stack.Pop();
                }
                else if (xVariable < 16)
                {
                    aArg = _machine.Memory.Stack[_machine.Memory.Stack.BP - xVariable];
                }
                else
                {
                    ushort xAddress = (ushort)(_machine.Story.Header.GlobalsOffset + 2 * (xVariable - 16));
                    _machine.Memory.GetWord(xAddress, out aArg);
                }
            }
            else if ((aType & 1) > 0) // Small Constant
            {
                _machine.Memory.CodeByte(out byte xValue);
                aArg = xValue;
            }
            else // Large Constant
            {
                _machine.Memory.CodeWord(out aArg);
            }

            _argCount++;
            ZDebug.Output($"  Storing operand: {_argCount - 1} -> {aArg}");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Copy value
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="copyField"></param>
        /// <returns></returns>
        public static bool CopyValueTo <TSource, TTarget>(this TSource source, TTarget target, bool copyField = false)
            where TSource : class
            where TTarget : TSource
        {
            if (source == null || target == null)
            {
                ZDebug.LogError("argument NULL");
                return(false);
            }

            foreach (PropertyInfo pi in source.GetType().GetProperties())
            {
                if (pi.CanRead && pi.CanWrite)
                {
                    pi.SetValue(target, pi.GetValue(source, null), null);
                }
            }

            if (copyField)
            {
                foreach (FieldInfo fi in source.GetType().GetFields())
                {
                    fi.SetValue(target, fi.GetValue(source));
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        public int RemoveFrame()
        {
            ZDebug.Output($"Removing Frame: {Frames}");

            SP = BP;
            Frames--;

            int callType = Pop();

            callType = callType >> 8;

            BP = Pop();
            BP++;

            long lowPC  = Pop();
            long highPC = Pop();

            highPC = highPC << 9;
            long pc = highPC | lowPC;

            _memory.PC = pc;
            if (_memory.PC < _memory.StartPC)
            {
                throw new Exception("Program counter is less than the start location!");
            }
            return(callType);
        }
Exemplo n.º 5
0
        private void InvokeOpcode_VAROP(Opcode aInstruction, int aOpcode, ushort aArg0, ushort aArg1, ushort aArg2, ushort aArg3, ushort aArg4, ushort aArg5, ushort aArg6, ushort aArg7, ushort aArgCount)
        {
            ZDebug.Output($"Invoking: {aOpcode.ToHex(2)} -> {aInstruction}:{aArgCount} -> {_invokeCount}");

            aInstruction.Execute(aArg0, aArg1, aArg2, aArg3, aArg4, aArg5, aArg6, aArg7, aArgCount);
            _invokeCount++;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Store an operand, either as a variable or pushed on the stack.
        /// </summary>
        /// <param name="value"></param>
        protected void Store(ushort value)
        {
            ZDebug.Output($"PC = {Machine.Memory.PC}");
            ZDebug.Output($"BP = {Machine.Memory.Stack.BP}");
            ZDebug.Output($"SP = {Machine.Memory.Stack.SP}");

            byte variable;

            Machine.Memory.CodeByte(out variable);

            if (variable == 0)
            {
                Machine.Memory.Stack.Push(value);
                ZDebug.Output($"  Storing {value} on stack at {Machine.Memory.Stack.SP}");
            }
            else if (variable < 16)
            {
                Machine.Memory.Stack[Machine.Memory.Stack.BP - variable] = value;
                ZDebug.Output($"  Storing {value} on stack as Variable {variable} at {Machine.Memory.Stack.SP}");
            }
            else
            {
                ushort addr = (ushort)(Machine.Story.Header.GlobalsOffset + 2 * (variable - 16));
                Machine.Memory.SetWord(addr, value);
                ZDebug.Output($"  Storing {value} at {addr}");
            }
        }
Exemplo n.º 7
0
        private void Dispatch()
        {
            if (_events.Count > 0)
            {
                IEvent evt = _events.Dequeue();
                if (!_listeners.ContainsKey(evt.Name))
                {
                    ZDebug.LogError("Event " + evt.Name + " has no listeners");
                    return;
                }

                for (int i = 0; i < _listeners[evt.Name].Count; i++)
                {
                    IEventListener listener = _listeners[evt.Name][i];
                    if (listener == null)
                    {
                        _listeners[evt.Name].RemoveAt(i);
                    }
                    else
                    {
                        listener.HandleEvent(evt);
                    }
                }
            }
        }
Exemplo n.º 8
0
        public void Notify <T>(T sender, string propName) where T : class
        {
            if (sender == null)
            {
                ZDebug.LogError(string.Format("Notify property sender is null! type: {0}, propertyName: {1}", typeof(T),
                                              propName));
                return;
            }
            if (string.IsNullOrEmpty(propName))
            {
                ZDebug.LogError(string.Format("Notify property Name cannot be null or empty! sender: {0}, propertyName: {1}", sender,
                                              propName));
                return;
            }

            PropertyInfo prop = sender.GetType().GetProperty(propName);

            if (prop == null)
            {
                ZDebug.LogError(string.Format("Notify property not found! sender: {0}, propertyName: {1}", sender,
                                              propName));
                return;
            }
            Notify(prop);
        }
Exemplo n.º 9
0
 public void DisposeAll()
 {
     foreach (IZDisposable disposable in _disposables)
     {
         if (disposable.Dispose())
         {
             ZDebug.Log(disposable.GetType() + " has been disposed");
         }
     }
 }
Exemplo n.º 10
0
 private void OnApplicationQuit()
 {
     foreach (IZDisposable disposable in _disposables)
     {
         if (disposable.DisposeOnApplicationQuit())
         {
             ZDebug.Log(disposable.GetType() + " has been disposed");
         }
     }
 }
Exemplo n.º 11
0
        protected void Branch(bool flag)
        {
            ZDebug.Output($"PC = {Machine.Memory.PC}");
            ZDebug.Output($"BP = {Machine.Memory.Stack.BP}");
            ZDebug.Output($"SP = {Machine.Memory.Stack.SP}");

            ushort offset;
            byte   specifier;
            byte   off1;
            byte   off2;

            Machine.Memory.CodeByte(out specifier);

            off1 = (byte)(specifier & 0x3f);

            if (!flag)
            {
                specifier ^= 0x80;
            }

            if ((specifier & 0x40) == 0)
            {
                if ((off1 & 0x20) > 0)
                {
                    off1 |= 0xc0;
                }

                Machine.Memory.CodeByte(out off2);
                offset = (ushort)((off1 << 8) | off2);
                ZDebug.Output($"long branch: {offset}");
            }
            else
            {
                offset = off1;
                ZDebug.Output($"short branch: {offset}");
            }

            if ((specifier & 0x80) > 0)
            {
                if (offset > 1)
                {
                    long pc = Machine.Memory.PC;
                    pc += (short)offset - 2;
                    Machine.Memory.PC = pc;
                    ZDebug.Output($"normal branch: {pc}");
                }
                else
                {
                    ZDebug.Output($"special branch: {offset}");
                    Return(offset);
                }
            }
        }
Exemplo n.º 12
0
        public void RemoveCommand <T>(string name) where T : ICommand
        {
            if (!HasCommand <T>(name))
            {
                ZDebug.LogError(string.Format("Error in {0} Command '{1}' not registered.", this, name));
                return;
            }

            lock (_commandList)
            {
                _commandList[name].Remove(typeof(T));
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Return from the current routine and restore the previous stack frame.
        /// The result may be stored (o), thrown away (1), or pushed on the stack (2).
        /// In the latter case a direct call has been finished and we must exit the interpreter loop.
        /// </summary>
        /// <param name="value"></param>
        protected void Return(ushort value)
        {
            ZDebug.Output($"PC = {Machine.Memory.PC}");
            ZDebug.Output($"BP = {Machine.Memory.Stack.BP}");
            ZDebug.Output($"SP = {Machine.Memory.Stack.SP}");

            int callType = Machine.Memory.Stack.RemoveFrame();

            if (callType == CallType.CallStore)
            {
                Store(value);
            }
        }
Exemplo n.º 14
0
 public ushort this[int index]
 {
     get
     {
         ZDebug.Output($"{index} -> {_data[index].ToString()}");
         return(_data[index]);
     }
     set
     {
         ZDebug.Output($"{index} <- {value}");
         _data[index] = value;
     }
 }
Exemplo n.º 15
0
        public void Interpret()
        {
            do
            {
                _machine.Memory.CodeByte(out byte opcode);
                long pc = _machine.Memory.PC;
                _argCount = 0;

                ZDebug.Output($"CODE: {pc - 1} -> {opcode.ToHex(2)}");

                ushort xArg0 = 0, xArg1 = 0, xArg2 = 0, xArg3 = 0, xArg4 = 0, xArg5 = 0, xArg6 = 0, xArg7 = 0;
                if (opcode < 0x80) // 2OP Opcodes
                {
                    LoadOperand((byte)((opcode & 0x40) > 0 ? 2 : 1), out xArg0);
                    LoadOperand((byte)((opcode & 0x20) > 0 ? 2 : 1), out xArg1);

                    int op = opcode & 0x1f;
                    InvokeOpcode_2OP(OP2Opcodes[op], opcode, xArg0, xArg1);
                }
                else if (opcode < 0xb0) // 1OP opcodes
                {
                    LoadOperand((byte)(opcode >> 4), out xArg0);

                    int op = opcode & 0x0f;
                    InvokeOpcode_1OP(OP1Opcodes[op], opcode, xArg0);
                }
                else if (opcode < 0xc0) // 0OP opcodes
                {
                    int op = opcode - 0xb0;
                    InvokeOpcode_0OP(OP0Opcodes[op], opcode);
                }
                else // VAR opcodes
                {
                    _machine.Memory.CodeByte(out byte xSpecifier1);
                    int xArgsCount = LoadAllOperands(xSpecifier1, out xArg0, out xArg1, out xArg2, out xArg3);

                    // Call opcodes with up to 8 arguments
                    if (opcode == 0xec || opcode == 0xfa)
                    {
                        _machine.Memory.CodeByte(out byte xSpecifier2);
                        xArgsCount += LoadAllOperands(xSpecifier2, out xArg4, out xArg5, out xArg6, out xArg7);
                    }

                    int op = opcode - 0xc0;
                    InvokeOpcode_VAROP(VAROpcodes[op], opcode, xArg0, xArg1, xArg2, xArg3, xArg4, xArg5, xArg6, xArg7, (ushort)xArgsCount);
                }

                _machine.Tick();
            } while (Finished == 0);
        }
Exemplo n.º 16
0
        public void RemoveModel(string name)
        {
            if (!HasModel(name))
            {
                ZDebug.LogError(string.Format("Error in {0} Model '{1}' not registered.", this, name));
                return;
            }

            lock (_models)
            {
                _models[name].Dispose();
                _models.Remove(name);
            }
        }
Exemplo n.º 17
0
        public void RemoveWire(string name)
        {
            if (!HasWire(name))
            {
                ZDebug.LogError(string.Format("Error in {0} Wire '{1}' not registered.", this, name));
                return;
            }

            lock (_wires)
            {
                _wires[name].Dispose();
                _wires.Remove(name);
            }
        }
Exemplo n.º 18
0
        public void RemoveView(string name)
        {
            if (!HasView(name))
            {
                ZDebug.Log(string.Format("Error in {0} View '{1}' don't registered.", this, name));
                return;
            }

            lock (_views)
            {
                _views[name].Dispose();
                _views.Remove(name);
            }
        }
Exemplo n.º 19
0
        public void AddFrame(int aArgCount, int aCallType)
        {
            long pc = _memory.PC;

            Push((ushort)(pc >> 9));
            Push((ushort)(pc & 0x1ff));
            Push((ushort)(BP - 1));
            Push((ushort)(aArgCount | (aCallType << 8)));

            BP = SP;
            Frames++;

            ZDebug.Output($"Added Frame: {Frames} -> {this[SP + 0]}:{this[SP + 1]}:{this[SP + 2]}:{this[SP + 3]}");
        }
Exemplo n.º 20
0
    IEnumerator Start()
    {
        // 这个GameManager需要自己实现
//		yield return StartCoroutine (GameManager.Instance ().OnStart ());
        ZConsole.Instance();
        ZDebug.Init();
        if (!APP_CONFIG.DEBUG)
        {
            ZDebug.DisplayType   = ZDebug.OutputType.File;
            ZDebug.DisplayMethod = ZDebug.ConsoleLogMethod.Selected;
        }
        ZDebug.SelectType <App>();
        ZDebug.Log("App Started!");
        yield return(null);
    }
Exemplo n.º 21
0
        /// <summary>
        /// Load mapped class
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public bool Load <T>()
        {
            ResetMap();

            //Check mapping
            if (!Attribute.IsDefined(typeof(T), typeof(CSVMapperAttribute), false))
            {
                ZDebug.LogError(string.Format("CSV mapping attribute not found in type: {0}", typeof(T)));
                return(false);
            }

            CSVMapperAttribute mapper =
                Attribute.GetCustomAttribute(typeof(T), typeof(CSVMapperAttribute), false) as CSVMapperAttribute;

            _keyRow   = mapper.KeyRow;
            _descRow  = mapper.DescRow;
            _startRow = mapper.StartRow;

            if (string.IsNullOrEmpty(mapper.Path))
            {
                ZDebug.LogError(string.Format("CSV path not found: {0}", mapper.Path));
                return(false);
            }

            //Read text
            TextAsset asset = Resources.Load <TextAsset>(mapper.Path);

            if (asset == null)
            {
                ZDebug.LogError(string.Format("CSV file not found: {0}", mapper.Path));
                return(false);
            }

            string content = asset.text;
            bool   result  = Load(content, mapper.Separator);

            if (result)
            {
                if (_keyRow < 0 || _records[_keyRow].Any(string.IsNullOrEmpty))
                {
                    ZDebug.LogError(
                        string.Format("Encoding Error! No key column found. Make sure target file is in UTF-8 format. Path: {0}",
                                      mapper.Path));
                    return(false);
                }
            }
            return(result);
        }
Exemplo n.º 22
0
    public void removeBlock(IntVector3 point)
    {
        if (!shipInfo.isInsideArray(point))
        {
            Debug.Log("removeBlock got request outside array " + ZDebug.toString(point));
            return;
        }
        if (!shipInfo.isBlockOccupied(point))
        {
            Debug.Log("removeBlock got unoccupied request " + ZDebug.toString(point));
            return;
        }

        ship3DView.removeBlock(point);
        shipModel.removeBlock(point);
    }
Exemplo n.º 23
0
    public void StateChange(IGameState newState)
    {
        if (newState == null)
        {
            ZDebug.LogError("State error!");
            return;
        }
        if (CurrentState != null)
        {
            CurrentState.OnExit();
        }

        CurrentState = newState;

        CurrentState.OnEnter();
    }
Exemplo n.º 24
0
        /// <summary>
        /// Load resource by key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Object Load(string key)
        {
            if (CanLoad)
            {
                ZResource.Resource resource = Instance._resource[key];
                if (resource != null && resource.resource != null)
                {
                    return(resource.resource);
                }

                ZDebug.LogError("Resource not found: " + key);
                return(null);
            }

            ZDebug.LogError("Resource cannot be loaded: " + key);
            return(null);
        }
Exemplo n.º 25
0
 /// <summary>
 /// Get raw value by CSVColumnAttribute or name
 /// </summary>
 /// <param name="attribute"></param>
 /// <param name="fields"></param>
 /// <param name="keys"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 private string GetRawValue(CSVColumnAttribute attribute, List <string> fields, List <string> keys, string name)
 {
     if (attribute.Column >= 0 && fields.Count > attribute.Column)
     {
         return(fields[attribute.Column]);
     }
     if (!string.IsNullOrEmpty(attribute.Key) && keys.Contains(attribute.Key))
     {
         return(fields[keys.IndexOf(attribute.Key)]);
     }
     if (keys.Contains(name))
     {
         return(fields[keys.IndexOf(name)]);
     }
     ZDebug.LogError(string.Format("Mapping Error! Column: {0}, Key: {1}, Name:{2}", attribute.Column,
                                   attribute.Key ?? "NULL", name));
     return(name);
 }
Exemplo n.º 26
0
    public void createBlock(int blockCode, IntVector3 position)
    {
        if (!shipInfo.isInsideArray(position))
        {
            Debug.Log("aborted createBlock. bad coordinates: " + ZDebug.toString(position));
            return;
        }
        if (shipInfo.isBlockOccupied(position))
        {
            Debug.Log("aborted createBlock. occupied coordinates: " + ZDebug.toString(position));
            return;
        }

        BlockData blockData = BlockDataLookup.getBlockDataByCode(blockCode).getCopy();
        Block     block     = ship3DView.createBlock(blockData, position);

        shipModel.createBlock(blockData, position);
    }
Exemplo n.º 27
0
        /// <summary>
        /// Decode CSV file to target mapped type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public IEnumerable <T> Decode <T>() where T : new()
        {
            if (_records == null || _keyRow < 0 || _descRow < 0 || _startRow < 0)
            {
                ZDebug.LogError(string.Format("Decoding Failed: {0}", typeof(T)));
                yield break;
            }

            //Decode each row
            for (int i = _startRow; i < _records.Count; i++)
            {
                if (i == _keyRow || i == _descRow)
                {
                    continue;
                }
                yield return(DecodeRow <T>(_records[i], _records[_keyRow]));
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// Load string content
        /// </summary>
        /// <param name="content"></param>
        /// <param name="separator"></param>
        /// <returns></returns>
        public bool Load(string content, char separator = ',')
        {
            //Dispose records
            ClearRecord();
            if (string.IsNullOrEmpty(content))
            {
                ZDebug.LogError(string.Format("CSV file content empty!"));
                return(false);
            }

            bool check = CheckLegal(content, separator);

            if (!check)
            {
                return(false);
            }

            Separator = separator;
            _records  = new List <List <string> >();
            foreach (string row in content.Split('\r').Where(line => !string.IsNullOrEmpty(line.Trim())))
            {
                List <string> columns = row.Split(separator).Select(s => s.Trim()).ToList();
                //Check each row's column count. They must match
                if (ColumnCount != 0 && columns.Count != ColumnCount)
                {
                    ZDebug.LogError(
                        string.Format("CSV parsing error at line {0} : columns counts do not match! Separator: '{1}'",
                                      content.IndexOf(row), separator));
                    return(false);
                }
                ColumnCount = columns.Count;
                _records.Add(columns);
            }
            RowCount = _records.Count;

            if (_records == null || !_records.Any())
            {
                ZDebug.LogWarning(string.Format("CSV file parsing failed(empty records)!"));
                return(false);
            }

            return(true);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Load resource by key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T Load <T>(string key) where T : Object
        {
            Object resource = Load(key);

            if (resource == null)
            {
                return(null);
            }

            T result = resource as T;

            if (result != null)
            {
                return(result);
            }

            ZDebug.LogError("Resource converting failed: " + key + " --> " + typeof(T));

            return(null);
        }
Exemplo n.º 30
0
        public void AddModel(IModel model)
        {
            if (model == null)
            {
                ZDebug.LogError(string.Format("Error in {0} Model can't be null.", this));
                return;
            }
            if (HasModel(model.Name))
            {
                ZDebug.LogError(string.Format("Error in {0} Model '{1}' already registered.", this, model.Name));
                return;
            }

            lock (_models)
            {
                _models[model.Name] = model;
                model.Dispatcher    = _dispatcher;
                model.Init();
            }
        }