Exemplo n.º 1
0
 private static void SaveOBS(ObsJson obs)
 {
     try
     {
         var ser = new DataContractJsonSerializer(typeof(ObsJson));
         var s   = new MemoryStream();
         ser.WriteObject(s, obs);
         File.WriteAllBytes(obs.filepath, s.ToArray());
     }
     catch (Exception e)
     {
         throw new Exception("Error saving obs: " + obs.filepath + "\r\n" + e.Message);
     }
 }
Exemplo n.º 2
0
        private void SelectMap(int index)
        {
            if ((index < 0) || (index >= maps_.Count))
            {
                return;
            }

            if (selected_map_ >= 0)
            {
                for (var i = 0; i < 13; ++i)
                {
                    fmf_data_.layers[i] = Convert.ToBase64String(map_layers_[i]);
                }

                fmfs_[selected_map_] = fmf_data_;
                obss_[selected_map_] = obs_data_;
            }

            selected_map_ = index;
            fmf_data_     = fmfs_[index];
            obs_data_     = obss_[index];

            MapListBox.SelectedIndexChanged  -= MapListBox_SelectedIndexChanged;
            MapDesignCB.SelectedIndexChanged -= MapDesignCB_SelectedIndexChanged;
            EventMapCB.SelectedIndexChanged  -= EventMapCB_SelectedIndexChanged;
            MapListBox.SelectedIndex          = index;
            MapDesignCB.SelectedIndex         = index;
            EventMapCB.SelectedIndex          = index;
            MapListBox.SelectedIndexChanged  += MapListBox_SelectedIndexChanged;
            MapDesignCB.SelectedIndexChanged += MapDesignCB_SelectedIndexChanged;
            EventMapCB.SelectedIndexChanged  += EventMapCB_SelectedIndexChanged;

            RefreshMad();
            RefreshFmf();
            RefreshEvent();
        }
Exemplo n.º 3
0
        // Avert thy gaze
        private void NewMapBT_Click(object sender, EventArgs e)
        {
            if ((maps_.Count == 0) || string.IsNullOrEmpty(working_dir_))
            {
                ErrMsg("No data loaded!");
                return;
            }

            using (var dialog = new NewMapDialog(map_names_.Length - 1))
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    var madjson  = new MadJson();
                    var fmfjson  = new FmfJson();
                    var obsjson  = new ObsJson();
                    var id       = dialog.MapID;
                    var name     = dialog.MapName;
                    var w        = dialog.MapW;
                    var h        = dialog.MapH;
                    var layer_sz = w * h * 2;

                    var idstr = id.ToString("D3");
                    var dir   = is_ynk_ ? @"\gn_dat5.arc\map\data\" : @"\gn_dat3.arc\map\data\";
                    dir = working_dir_ + dir + idstr + @"\";

                    madjson.id       = id;
                    madjson.filepath = dir + idstr + ".json";
                    var madpath = dir + idstr + ".mad";
                    fmfjson.id       = id;
                    fmfjson.filepath = dir + idstr + "_fmf.json";
                    var fmfpath = dir + idstr + ".fmf";
                    obsjson.id       = id;
                    obsjson.filepath = dir + idstr + "_obs.json";
                    var obspath = dir + idstr + ".obs";

                    if (File.Exists(madpath))
                    {
                        ErrMsg("File already exists: " + madpath);
                        return;
                    }
                    if (File.Exists(fmfpath))
                    {
                        ErrMsg("File already exists: " + fmfpath);
                        return;
                    }
                    if (File.Exists(obspath))
                    {
                        ErrMsg("File already exists: " + obspath);
                        return;
                    }
                    if (!Directory.Exists(dir))
                    {
                        try
                        {
                            Directory.CreateDirectory(dir);
                        }
                        catch
                        {
                            ErrMsg("Failed to create directory: " + dir);
                            return;
                        }
                    }

                    // FMF
                    fmfjson.width          = (uint)w;
                    fmfjson.height         = (uint)h;
                    fmfjson.num_layers     = 13;
                    fmfjson.payload_length = (uint)layer_sz * 13;
                    fmfjson.unknown_1      = 0x20;
                    fmfjson.unknown_2      = 0x20;
                    fmfjson.unknown_3      = 0x10;

                    var fmf_sz  = fmfjson.payload_length + 20;
                    var fmfdata = new byte[fmf_sz];
                    Array.Copy(new byte[] { (byte)'F', (byte)'M', (byte)'F', (byte)'_' }, fmfdata, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.payload_length), 0, fmfdata, 4, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.width), 0, fmfdata, 8, 4);
                    Array.Copy(BitConverter.GetBytes(fmfjson.height), 0, fmfdata, 0x0C, 4);
                    fmfdata[0x10] = 0x20;
                    fmfdata[0x11] = 0x20;
                    fmfdata[0x12] = 13;
                    fmfdata[0x13] = 0x10;
                    for (var i = 0; i < 13; ++i)
                    {
                        fmfjson.layers[i] = Convert.ToBase64String(fmfdata, 20, layer_sz);
                    }
                    File.WriteAllBytes(fmfpath, fmfdata);
                    SaveFMF(fmfjson);

                    // OBS
                    obsjson.entries = new ObsEntry[1024];
                    for (var i = 0; i < 1024; ++i)
                    {
                        var entry = new ObsEntry();
                        entry.index          = (uint)i;
                        entry.event_index    = (uint)i;
                        entry.movement_delay = 255;
                        entry.flags[0]       = 1;
                        entry.flags[1]       = 1;
                        obsjson.entries[i]   = entry;
                    }
                    File.WriteAllBytes(obspath, new byte[1024 * 20]);
                    SaveOBS(obsjson);

                    // MAD
                    madjson.location_name = name;
                    for (var i = 0; i < 10; ++i)
                    {
                        madjson.normal_encounters[i] = new MadEncounter();
                        if (i < 5)
                        {
                            madjson.special_encounters[i] = new MadEncounter();
                        }
                    }
                    File.WriteAllBytes(madpath, new byte[0x8B]);
                    SaveMAD(madjson);

                    if (id < map_names_.Length)
                    {
                        map_names_[id] = name;
                    }
                    maps_.Add(madjson);
                    fmfs_.Add(fmfjson);
                    obss_.Add(obsjson);

                    MapListBox.Items.Add(name);
                    MapDesignCB.Items.Add(name);
                    EventMapCB.Items.Add(name);

                    for (var i = 0; i < 128; ++i)
                    {
                        event_flags_[(id * 128) + i] = 0xFF;
                    }

                    SelectMap(maps_.Count - 1);
                }
            }
        }
Exemplo n.º 4
0
        private void LoadMaps(string dir)
        {
            DirectoryInfo di = new DirectoryInfo(dir);

            if (!di.Exists)
            {
                throw new Exception("Could not locate map folder");
            }

            maps_.Clear();
            fmfs_.Clear();
            obss_.Clear();

            try
            {
                var   files = di.GetFiles("*.json", SearchOption.AllDirectories);
                Regex rx    = new Regex(@"[\\/]+(?<id>\d{3})[\\/]+(\k<id>)\.json$", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                Parallel.ForEach(files, file => {
                    var match = rx.Match(file.FullName);

                    if (match.Success)
                    {
                        var fmf = file.FullName.Replace(".json", "_fmf.json");
                        var obs = file.FullName.Replace(".json", "_obs.json");
                        if (!File.Exists(fmf) || !File.Exists(obs))
                        {
                            return;
                        }

                        var id = int.Parse(match.Groups["id"].Value);
                        Parallel.Invoke(() => { LoadMad(file.FullName, id); },
                                        () => { LoadFmf(fmf, id); },
                                        () => { LoadObs(obs, id); });
                    }
                });
            }
            catch (Exception e)
            {
                throw new Exception("Error iterating map folder: " + e.Message);
            }

            maps_.Sort((MadJson x, MadJson y) => { return(x.id.CompareTo(y.id)); });
            fmfs_.Sort((FmfJson x, FmfJson y) => { return(x.id.CompareTo(y.id)); });
            obss_.Sort((ObsJson x, ObsJson y) => { return(x.id.CompareTo(y.id)); });

            MapListBox.ClearSelected();
            MapListBox.Items.Clear();

            MapNameTextBox.Clear();
            MapFilenameTextBox.Clear();
            ClearEncounter();

            fmf_data_     = null;
            obs_data_     = null;
            map_layers_   = new byte[13][];
            selected_map_ = -1;

            foreach (var map in maps_)
            {
                MapListBox.Items.Add(map.location_name);
            }

            MapMusicCB.SelectedIndex = -1;
            MapMusicCB.Items.Clear();
            MapMusicCB.DisplayMember = "Item1";
            MapMusicCB.ValueMember   = "Item2";
            foreach (var bgm in bgm_data_)
            {
                MapMusicCB.Items.Add(new Tuple <string, uint>(bgm.Value, bgm.Key));
            }
        }