Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if a TorchBase instance already exists.</exception>
        protected TorchBase()
        {
            if (Instance != null)
            {
                throw new InvalidOperationException("A TorchBase instance already exists.");
            }

            Instance = this;

            TorchVersion        = Assembly.GetExecutingAssembly().GetName().Version;
            TorchVersionVerbose = Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? TorchVersion.ToString();
            RunArgs             = new string[0];

            Managers = new DependencyManager();

            Plugins     = new PluginManager(this);
            Multiplayer = new MultiplayerManager(this);
            Entities    = new EntityManager(this);
            Network     = new NetworkManager(this);
            Commands    = new CommandManager(this);

            Managers.AddManager(new TorchSessionManager(this));
            Managers.AddManager(new FilesystemManager(this));
            Managers.AddManager(new UpdateManager(this));
            Managers.AddManager(Network);
            Managers.AddManager(Commands);
            Managers.AddManager(Plugins);
            Managers.AddManager(Multiplayer);
            Managers.AddManager(Entities);
            Managers.AddManager(new ChatManager(this));

            TorchAPI.Instance = this;
        }
Пример #2
0
 public bool AddManager <T>(T manager) where T : class, IManager
 {
     return(Managers.AddManager(manager));
 }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown if a TorchBase instance already exists.</exception>
        protected TorchBase(ITorchConfig config)
        {
            RegisterCoreAssembly(GetType().Assembly);
            if (Instance != null)
            {
                throw new InvalidOperationException("A TorchBase instance already exists.");
            }

            Instance = this;
            Config   = config;

            var versionString = Assembly.GetEntryAssembly()
                                .GetCustomAttribute <AssemblyInformationalVersionAttribute>()
                                .InformationalVersion;

            if (!InformationalVersion.TryParse(versionString, out InformationalVersion version))
            {
                throw new TypeLoadException("Unable to parse the Torch version from the assembly.");
            }

            TorchVersion = version;

            RunArgs = new string[0];

            Managers = new DependencyManager();

            Plugins = new PluginManager(this);

            var sessionManager = new TorchSessionManager(this);

            sessionManager.AddFactory((x) => Sync.IsServer ? new ChatManagerServer(this) : new ChatManagerClient(this));
            sessionManager.AddFactory((x) => Sync.IsServer ? new CommandManager(this) : null);
            sessionManager.AddFactory((x) => new EntityManager(this));

            Managers.AddManager(sessionManager);
            Managers.AddManager(new PatchManager(this));
            Managers.AddManager(new FilesystemManager(this));
            Managers.AddManager(new UpdateManager(this));
            Managers.AddManager(new EventManager(this));
            Managers.AddManager(Plugins);
            TorchAPI.Instance = this;

            GameStateChanged += (game, state) =>
            {
                if (state == TorchGameState.Created)
                {
                    // If the attached assemblies change (MySandboxGame.ctor => MySandboxGame.ParseArgs => MyPlugins.RegisterFromArgs)
                    // attach assemblies to object factories again.
                    ObjectFactoryInitPatch.ForceRegisterAssemblies();
                    // safe to commit here; all important static ctors have run
                    PatchManager.CommitInternal();
                }
            };

            sessionManager.SessionStateChanged += (session, state) =>
            {
                switch (state)
                {
                case TorchSessionState.Loading:
                    SessionLoading?.Invoke();
                    break;

                case TorchSessionState.Loaded:
                    SessionLoaded?.Invoke();
                    break;

                case TorchSessionState.Unloading:
                    SessionUnloading?.Invoke();
                    break;

                case TorchSessionState.Unloaded:
                    SessionUnloaded?.Invoke();
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(state), state, null);
                }
            };
        }