private void CreateWorld(CreateWorldForm form, int numMutants, int mutantSize) { try { Directory.CreateDirectory(worldPath); var connectionString = new SQLiteConnectionStringBuilder() { DataSource = dbPath }.ConnectionString; var conn = new SQLiteConnection(connectionString); conn.Open(); dbContext = new WorldDbContext(conn, false); dbContext.Create(numMutants, mutantSize); mutantManager = new MutantManager(dbContext, worldPath); mutantManager.CreateMutants( x => { form.Progress = x; Application.DoEvents(); }); form.Closed -= OnCreateWorldFormClosed; ShowWorld(); form.Close(); } catch (Exception e) { DestroyWorld(); while (e.InnerException != null) { e = e.InnerException; } MessageBox.Show("Coulnd't create or open db file, because " + e.Message, "Fatal Error"); Environment.Exit(-1); } }
private void DestroyWorld() { if (dbContext != null) { mutantManager = null; dbContext.Database.Connection.Close(); dbContext.Dispose(); dbContext = null; GC.Collect(); GC.WaitForPendingFinalizers(); } Directory.Delete(worldPath, true); }
public EvolutionRunner(WorldDbContext dbContext, MutantManager mutantManager, EvolverSettings settings, int[] mutants = null) { this.dbContext = dbContext; this.mutantManager = mutantManager; this.settings = settings; this.mutants = mutants; numMutants = mutants == null ? dbContext.NumMutants : mutants.Length; mutantMemory = new sbyte[numMutants][]; SetUpMap(); mutantsByScore = new SortedDictionary <long, HashSet <int> >(); for (int i = 0; i < numMutants; ++i) { var mutantStat = dbContext.Mutants.Find(GetMutantId(i)); AddMutantScore(i, mutantStat.Score); } }
public EvolverApplicationContext() { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; worldPath = Path.Combine(Application.StartupPath, "world"); dbPath = Path.Combine(worldPath, "world.db"); if (Directory.Exists(worldPath)) { try { Directory.CreateDirectory(worldPath); var connectionString = new SQLiteConnectionStringBuilder() { DataSource = dbPath }.ConnectionString; var conn = new SQLiteConnection(connectionString); conn.Open(); dbContext = new WorldDbContext(conn, true); mutantManager = new MutantManager(dbContext, worldPath); mutantManager.LoadMutants(); ShowWorld(); } catch (Exception e) { while (e.InnerException != null) { e = e.InnerException; } MessageBox.Show("Failed to load world: " + e.Message, "Fatal Error"); Environment.Exit(-1); } } else { var createWorldForm = new CreateWorldForm(); createWorldForm.Closed += OnCreateWorldFormClosed; createWorldForm.CreateWorldHandler = CreateWorld; createWorldForm.Show(); } }