示例#1
0
 public bool TryGet(string name, out MultiVerseInstance instance)
 {
     return(MultiVerses.TryGetValue(name, out instance));
 }
示例#2
0
        public CreateResult Load(string levelName, Action <MultiVerseInstance> callback = null)
        {
            string worldFolder = Path.Combine(WorldFolder, levelName);
            string mvInfoPath  = Path.Combine(worldFolder, "mvInfo.json");

            if (!File.Exists(mvInfoPath))
            {
                return(CreateResult.InvalidWorld);
            }

            bool lockEntered = false;

            try
            {
                if (!_busy && Monitor.TryEnter(_createLock))
                {
                    lockEntered = true;

                    try
                    {
                        var mvInfo = JsonConvert.DeserializeObject <MvFile>(File.ReadAllText(mvInfoPath));
                        var type   = Type.GetType(mvInfo.GeneratorType);

                        if (!GeneratorManager.TryGet(type, out var generator))
                        {
                            return(CreateResult.GeneratorUnavailable);
                        }

                        var wp = new AnvilWorldProvider(worldFolder)
                        {
                            MissingChunkProvider = generator
                        };

                        var level = new MultiVerseLevel(Api, LevelManager, levelName,
                                                        wp, LevelManager.EntityManager);

                        MultiVerseInstance mv;
                        if (!MultiVerses.TryAdd(levelName,
                                                mv = new MultiVerseInstance(levelName, level, generator, mvInfo.GeneratorType)))
                        {
                            level.Close();
                            return(CreateResult.NameTaken);
                        }

                        _busy = true;

                        //level.Initialize();
                        mv.Initialize().ContinueWith((o) =>
                        {
                            wp.SaveChunks();

                            mv.Ready = true;
                            callback?.Invoke(mv);

                            _busy = false;
                        });

                        return(CreateResult.Initializing);
                    }
                    catch (Exception ex)
                    {
                        Log.Warn($"Failed to load world!", ex);
                        return(CreateResult.UnknownError);
                    }
                }
            }
            finally
            {
                if (lockEntered)
                {
                    Monitor.Exit(_createLock);
                }
            }

            return(CreateResult.Busy);
        }
示例#3
0
        public CreateResult Create(string levelName, string worldGenerator, Action <MultiVerseInstance> callback = null)
        {
            bool enteredLock = false;

            try
            {
                if (!_busy && Monitor.TryEnter(_createLock))
                {
                    enteredLock = true;

                    try
                    {
                        if (!GeneratorManager.TryGet(worldGenerator, out IWorldGenerator generator))
                        {
                            return(CreateResult.InvalidWorldGenerator);
                        }

                        if (MultiVerses.ContainsKey(levelName))
                        {
                            return(CreateResult.NameTaken);
                        }

                        string worldFolder = Path.Combine(WorldFolder, levelName);
                        string mvInfoPath  = Path.Combine(worldFolder, "mvInfo.json");

                        if (!Directory.Exists(worldFolder))
                        {
                            Directory.CreateDirectory(worldFolder);
                            File.WriteAllText(mvInfoPath, JsonConvert.SerializeObject(new MvFile()
                            {
                                LevelName     = levelName,
                                GeneratorType = generator.GetType().AssemblyQualifiedName
                            }));
                        }

                        var wp = new AnvilWorldProvider(worldFolder)
                        {
                            MissingChunkProvider = generator
                        };

                        var level = new MultiVerseLevel(Api, LevelManager, levelName,
                                                        wp, LevelManager.EntityManager);

                        MultiVerseInstance mv;
                        if (!MultiVerses.TryAdd(levelName,
                                                mv = new MultiVerseInstance(levelName, level, generator, worldGenerator)))
                        {
                            level.Close();
                            return(CreateResult.NameTaken);
                        }

                        _busy = true;

                        //level.Initialize();
                        mv.Initialize().ContinueWith((o) =>
                        {
                            wp.SaveChunks();

                            mv.Ready = true;
                            callback?.Invoke(mv);

                            _busy = false;
                        });

                        return(CreateResult.Initializing);
                    }
                    catch (Exception ex)
                    {
                        _busy = false;
                        return(CreateResult.UnknownError);
                    }
                }
            }
            finally
            {
                if (enteredLock)
                {
                    Monitor.Exit(_createLock);
                }
            }

            return(CreateResult.Busy);
        }