コード例 #1
0
 public void RemoveTreasureHunt(TreasureHunt.TreasureHunt treasureHunt)
 {
     if (!removeWorker.IsBusy)
     {
         removeWorker.RunWorkerAsync(treasureHunt);
     }
 }
コード例 #2
0
    private void RemoveWorkerDoWork(object sender, DoWorkEventArgs e)
    {
        TreasureHunt.TreasureHunt treasureHunt = e.Argument as TreasureHunt.TreasureHunt;

        string filePath = persistentDataPath + "/" + treasureHunt.Title + Constants.Extension;

        if (File.Exists(filePath))
        {
            try
            {
                File.Delete(filePath);
            }
            catch (UnauthorizedAccessException exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }
            catch (IOException exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }
            catch (Exception exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }
        }
    }
コード例 #3
0
 private void SaveFile(TreasureHunt.TreasureHunt treasureHunt, string filePath)
 {
     try
     {
         using (Stream stream = File.Open(filePath, FileMode.Create, FileAccess.Write))
         {
             BinaryFormatter binFormatter = new BinaryFormatter();
             binFormatter.Serialize(stream, treasureHunt);
         }
     }
     catch (ArgumentException e)
     {
         MonoBehaviour.print(e.Message); // TODO actually let the player know of the problem
     }
     catch (IOException e)
     {
         MonoBehaviour.print(e.Message); // TODO actually let the player know of the problem
     }
     catch (UnauthorizedAccessException e)
     {
         MonoBehaviour.print(e.Message); // TODO actually let the player know of the problem
     }
     catch (SerializationException e)
     {
         MonoBehaviour.print(e.Message); // TODO actually let the player know of the problem
     }
     catch (Exception e)
     {
         MonoBehaviour.print(e.Message); // TODO actually let the player know of the problem
     }
 }
コード例 #4
0
 public void SaveTreasureHunt(TreasureHunt.TreasureHunt treasureHunt, string oldTitle = null) // Main thread
 {
     this.oldTitle = oldTitle;
     if (!saveWorker.IsBusy)
     {
         saveWorker.RunWorkerAsync(treasureHunt);
     }
 }
コード例 #5
0
    /// <summary>
    /// Creates a new, unnamed treasure hunt, and saves it.
    /// </summary>
    public void CreateTreasureHunt()
    {
        // TODO make better naming system for treasure hunts, problems and tasks
        TreasureHunt.TreasureHunt newTreasureHunt = new TreasureHunt.TreasureHunt("Unnamed Treasure Hunt " + (AllTreasureHunts.Count + 1));
        AllTreasureHunts.Add(newTreasureHunt);
        CurrentTreasureHunt = newTreasureHunt;

        if (TreasureHuntCreated != null)
        {
            TreasureHuntCreated();
        }

        SaveTreasureHunt();
    }
コード例 #6
0
    private void LoadWorkerDoWork(object sender, DoWorkEventArgs e) // Not on the main thread
    {
        List <TreasureHunt.TreasureHunt> treasureHunts = new List <TreasureHunt.TreasureHunt>();

        string searchPattern = "*" + Constants.Extension;

        try
        {
            string[] allTreasureHuntsPaths = Directory.GetFiles(persistentDataPath, searchPattern);

            for (int i = 0; i < allTreasureHuntsPaths.Length; i++)
            {
                using (Stream stream = File.Open(allTreasureHuntsPaths[i], FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter binFormatter = new BinaryFormatter();

                    TreasureHunt.TreasureHunt treasureHunt = binFormatter.Deserialize(stream) as TreasureHunt.TreasureHunt;
                    if (treasureHunt != null)
                    {
                        treasureHunts.Add(treasureHunt);
                    }
                }
            }

            e.Result = treasureHunts;
        }
        catch (ArgumentException exc)
        {
            MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
        }
        catch (IOException exc)
        {
            MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
        }
        catch (UnauthorizedAccessException exc)
        {
            MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
        }
        catch (SerializationException exc)
        {
            MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
        }
        catch (Exception exc)
        {
            MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
        }
    }
コード例 #7
0
    private void SaveWorkerDoWork(object sender, DoWorkEventArgs e) // IS in fact on a separate thread
    {
        TreasureHunt.TreasureHunt treasureHunt = e.Argument as TreasureHunt.TreasureHunt;
        if (treasureHunt == null)
        {
            throw new ArgumentException("You must use a TreasureHunt as argument for DoWorkEventArgs");
        }

        string filePath    = persistentDataPath + "/" + treasureHunt.Title + Constants.Extension;
        string oldFilePath = persistentDataPath + "/" + oldTitle + Constants.Extension;

        // Normal save
        SaveFile(treasureHunt, filePath);

        if (oldTitle != null && File.Exists(oldFilePath))
        {
            // Treasure Hunt was renamed and saved as a new file so the old one is deleted
            try
            {
                File.Delete(oldFilePath);
            }
            catch (UnauthorizedAccessException exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }
            catch (IOException exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }
            catch (Exception exc)
            {
                MonoBehaviour.print(exc.Message); // TODO actually let the player know of the problem
            }


            // Necessary for the next method call
            oldTitle = null;
        }
    }
コード例 #8
0
 public void RemoveTreasureHunt(TreasureHunt.TreasureHunt treasureHunt)
 {
     AllTreasureHunts.Remove(treasureHunt);
     PersistenceService.Instance.RemoveTreasureHunt(treasureHunt);
 }