Example #1
0
        public void Mount(LuaMount mount, FilePath location, FilePath subPath = default(FilePath), bool readOnly = false)
        {
            // Check conditions
            if (location.IsBackPath)
            {
                throw new IOException(string.Format("Invalid location: {0}", location));
            }
            if (subPath.IsBackPath)
            {
                throw new IOException(string.Format("Invalid subPath: {0}", subPath));
            }
            location = location.UnRoot();
            subPath  = subPath.UnRoot();

            // Create the mount
            var info = new MountInfo(location, mount, subPath, readOnly);

            CheckEitherExists(new MountFilePath(info, subPath));

            // Add the mount
            m_mounts.Add(info);
            for (int i = m_mounts.Count - 1; i >= 0; --i)
            {
                var mountInfo = m_mounts[i];
                if (mountInfo != info && mountInfo.Location == location)
                {
                    m_mounts.RemoveAt(i);
                    mountInfo.Dispose();
                    break;
                }
            }
        }
Example #2
0
        public FileSystem()
        {
            var rootMount = new LuaMount(new EmptyMount("root"));

            rootMount.Connected = true;
            m_emptyRootMount    = new MountInfo(FilePath.Empty, rootMount, FilePath.Empty, true);
            m_mounts            = new List <MountInfo>();
        }
Example #3
0
            public MountInfo(FilePath location, LuaMount mount, FilePath subPath, bool readOnly)
            {
                m_luaMount = new LuaObjectRef <LuaMount>(mount);
                Location   = location;
                SubPath    = subPath;
                ReadOnly   = readOnly;

                LastReadTime  = DateTime.MinValue;
                LastWriteTime = DateTime.MinValue;
            }
Example #4
0
 public void Unmount(LuaMount mount)
 {
     // Remove the mount
     for (int i = m_mounts.Count - 1; i >= 0; --i)
     {
         var mountInfo = m_mounts[i];
         if (mountInfo.LuaMount == mount)
         {
             m_mounts.RemoveAt(i);
             mountInfo.Dispose();
         }
     }
 }
Example #5
0
 public void GetMountAccessTimes(LuaMount mount, out DateTime o_lastReadTime, out DateTime o_lastWriteTime)
 {
     o_lastReadTime  = DateTime.MinValue;
     o_lastWriteTime = DateTime.MinValue;
     for (int i = m_mounts.Count - 1; i >= 0; --i)
     {
         var mountInfo = m_mounts[i];
         if (mountInfo.LuaMount == mount)
         {
             if (mountInfo.LastReadTime > o_lastReadTime)
             {
                 o_lastReadTime = mountInfo.LastReadTime;
             }
             if (mountInfo.LastWriteTime > o_lastWriteTime)
             {
                 o_lastWriteTime = mountInfo.LastWriteTime;
             }
         }
     }
 }
Example #6
0
 internal void RequestBoot(LuaMount mount)
 {
     m_updateResult        = CPUUpdateResult.Boot;
     m_nextBootMount.Value = mount;
 }
Example #7
0
        private CPUUpdateResult ReallyBoot(LuaMount bootMount)
        {
            if (m_machine != null)
            {
                throw new InvalidOperationException();
            }

            // Check if the system has any RAM
            if (m_computer.Memory.TotalMemory == 0)
            {
                m_computer.ErrorOutput.WriteLine("Error starting CPU: No RAM");
                return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue);
            }

            // Mount the ROM
            m_fileSystem.Mount(bootMount, FilePath.Empty, FilePath.Empty, true);

            // Check if the boot path exists
            var bootPath = new FilePath("boot.lua");

            if (!m_fileSystem.Exists(bootPath) || m_fileSystem.IsDir(bootPath))
            {
                m_computer.ErrorOutput.WriteLine("Error starting CPU: ROM does not countain {0}", bootPath);
                m_fileSystem.UnmountAll();
                return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue);
            }

            // Init lua machine
            m_machine = new LuaMachine(m_computer.Memory);
            m_machine.AllowByteCodeLoading = false;
            m_machine.EnforceTimeLimits    = true;
            try
            {
                // Remove default APIs we don't want
                m_machine.RemoveUnsafeGlobals();

                // Install basic APIs
                if (m_computer.Host != null)
                {
                    m_machine.SetGlobal("_HOST", m_computer.Host);
                }
                InstallAPI(new IOAPI(m_computer, m_fileSystem));
                InstallAPI(new OSAPI(m_computer, this, m_fileSystem));
                InstallAPI(new PackageAPI(m_computer, m_fileSystem));

                // Install custom APIs
                PreloadAPI(new SystemAPI(m_computer, this));
                PreloadAPI(new FSAPI(m_computer, m_fileSystem));
            }
            catch (LuaError e)
            {
                m_computer.ErrorOutput.WriteLine("Error starting CPU: {0}", e.Message);
                Halt();
                return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue);
            }

            // Load the boot script
            try
            {
                string bootScript;
                using (var reader = m_fileSystem.OpenForRead(bootPath))
                {
                    bootScript = reader.ReadToEnd();
                }
                var bootFunction = m_machine.LoadString(bootScript, "@" + bootPath);
                m_mainRoutine = m_machine.CreateCoroutine(bootFunction);
                m_eventFilter.Clear();
            }
            catch (IOException e)
            {
                m_computer.ErrorOutput.WriteLine("Error loading {0}: {1}", bootPath, e.Message);
                Halt();
                return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue);
            }
            catch (LuaError e)
            {
                m_computer.ErrorOutput.WriteLine("Error parsing {0}: {1}", bootPath, e.Message);
                Halt();
                return(m_isMainCPU ? CPUUpdateResult.Shutdown : CPUUpdateResult.Continue);
            }

            // Start the boot script
            m_eventFilter.Clear();
            return(Resume(LuaArgs.Empty));
        }