|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/GoMudEngine/GoMud/internal/configs" |
| 11 | + "github.com/GoMudEngine/GoMud/internal/mudlog" |
| 12 | + "github.com/GoMudEngine/GoMud/internal/rooms" |
| 13 | + "gopkg.in/yaml.v2" |
| 14 | +) |
| 15 | + |
| 16 | +// Description: |
| 17 | +// rooms.Room.ZoneConfig was removed when Zone data was migrated to zone-config.yaml in zone folders |
| 18 | +// This function loads all of the yaml files in the DATAFILES/world/*/rooms/* and looks for any ZoneConfig data. |
| 19 | +// If found, the data is moved to a zone-config.yaml file, and the ZoneConfig data in the Room datafile is removed. |
| 20 | +func migrate_RoomZoneConfig() error { |
| 21 | + |
| 22 | + // This struct is how ZoneConfig looked as of 0.9.1 |
| 23 | + // Since we will be upgrading an older version to this format, use a copy of the struct from that period |
| 24 | + // To ensure we aren't using a struct that has changed over time |
| 25 | + type zoneConfig_1_0_0 struct { |
| 26 | + Name string `yaml:"name,omitempty"` |
| 27 | + RoomId int `yaml:"roomid,omitempty"` |
| 28 | + MobAutoScale struct { |
| 29 | + Minimum int `yaml:"minimum,omitempty"` // level scaling minimum |
| 30 | + Maximum int `yaml:"maximum,omitempty"` // level scaling maximum |
| 31 | + } `yaml:"autoscale,omitempty"` // level scaling range if any |
| 32 | + Mutators []struct { |
| 33 | + MutatorId string `yaml:"mutatorid,omitempty"` // Short text that will uniquely identify this modifier ("dusty") |
| 34 | + SpawnedRound uint64 `yaml:"spawnedround,omitempty"` // Tracks when this mutator was created (useful for decay) |
| 35 | + DespawnedRound uint64 `yaml:"despawnedround,omitempty"` // Track when it decayed to nothing. |
| 36 | + } `yaml:"mutators,omitempty"` |
| 37 | + IdleMessages []string `yaml:"idlemessages,omitempty"` // list of messages that can be displayed to players in the zone, assuming a room has none defined |
| 38 | + MusicFile string `yaml:"musicfile,omitempty"` // background music to play when in this zone |
| 39 | + DefaultBiome string `yaml:"defaultbiome,omitempty"` // city, swamp etc. see biomes.go |
| 40 | + RoomIds map[int]struct{} `yaml:"-"` // Does not get written. Built dyanmically when rooms are loaded. |
| 41 | + } |
| 42 | + |
| 43 | + c := configs.GetConfig() |
| 44 | + |
| 45 | + worldfilesGlob := filepath.Join(string(c.FilePaths.DataFiles), "rooms", "*", "*.yaml") |
| 46 | + matches, err := filepath.Glob(worldfilesGlob) |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + |
| 52 | + existingZoneFiles := map[string]struct{}{} |
| 53 | + |
| 54 | + // We only care about room files, so ###.yaml (possible negative) |
| 55 | + re := regexp.MustCompile(`^[\-0-9]+\.yaml$`) |
| 56 | + for _, path := range matches { |
| 57 | + |
| 58 | + // |
| 59 | + // Must look like a room yaml file: |
| 60 | + // 1.yaml |
| 61 | + // 123.yaml |
| 62 | + // -83.yaml |
| 63 | + // etc. |
| 64 | + // |
| 65 | + |
| 66 | + if !re.MatchString(filepath.Base(path)) { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + // |
| 71 | + // strip the filename form the room file and replace with zone-config.yaml |
| 72 | + // to get the path to the zone-config.yaml |
| 73 | + // |
| 74 | + zoneFilePath := filepath.Join(filepath.Dir(path), "zone-config.yaml") |
| 75 | + |
| 76 | + // |
| 77 | + // The following checks whether the zone config file already exists |
| 78 | + // We will leave the config data in the room data file if the zone-config.yaml is already present. |
| 79 | + // It should be inert if present, since it is not unmarshalled into anything in current code. |
| 80 | + // |
| 81 | + |
| 82 | + // Check whether zone file already is tracked as existing, if found, skip. |
| 83 | + if _, ok := existingZoneFiles[zoneFilePath]; ok { |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + _, err = os.Stat(zoneFilePath) |
| 88 | + if err == nil { |
| 89 | + // Mark zone file as existing, skip further processing. |
| 90 | + existingZoneFiles[zoneFilePath] = struct{}{} |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + // |
| 95 | + // End check for existing zone-config.yaml |
| 96 | + // After this point, we will unmarshal the yaml file into a generic map structure. |
| 97 | + // This allows us to examine the data in the yaml file, particularly the "zoneconfig" node |
| 98 | + // since the ZoneConfig field has been removed from the rooms.Room struct |
| 99 | + // We can de-populate the field, move it, and re-write the yaml back to the original room template file. |
| 100 | + // The downside to this method is that being a map, the fields will be read/written in a non-deterministic manner, |
| 101 | + // So the room yaml file field orders may be written in a random order. |
| 102 | + // Because of this, and as a final fix, we will finally marshal/unmarshal into the proper room struct from the map data |
| 103 | + // Allowing us to write the data in an expected ordered form. |
| 104 | + // |
| 105 | + |
| 106 | + data, err := os.ReadFile(path) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + // |
| 112 | + // First do a simple check for the field name in the text file. |
| 113 | + // We know the way the field will appear: "zoneconfig:" |
| 114 | + // This avoids having to unmarshal the struct and search that way, unnecessarily. |
| 115 | + // |
| 116 | + if !strings.Contains(string(data), "zoneconfig:") { |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + // |
| 121 | + // Unmarshal the entire yaml file into a map |
| 122 | + // This will let us further examine the data, modify it, etc. |
| 123 | + // |
| 124 | + filedata := map[string]any{} |
| 125 | + err = yaml.Unmarshal(data, &filedata) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to parse YAML: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + // Make sure that the zoneconfig key is present and populated |
| 131 | + if filedata[`zoneconfig`] == nil { |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + mudlog.Info("Migration 0.9.1", "file", path, "message", "migrating zoneconfig from room data file to zone-config.yaml") |
| 136 | + |
| 137 | + // |
| 138 | + // From here on out, this code migrates zoneconfig data out of room file and into zone-config.yaml |
| 139 | + // |
| 140 | + roomFileInfo, _ := os.Stat(path) |
| 141 | + |
| 142 | + mudlog.Info("Migration 0.9.1", "file", path, "message", "isolating zoneconfig data") |
| 143 | + |
| 144 | + // |
| 145 | + // Isolate the zoneconfig and write it to its own zone-config.yaml file |
| 146 | + // We'll marshal just the zoneconfig data, get its bytes, then unmarshal it into |
| 147 | + // the desired target structure. |
| 148 | + // Some fields have changed or are missing due to some slight differences in the new struct |
| 149 | + // so we'll also try and reconcile some of that by pulling from the core room definition |
| 150 | + // |
| 151 | + zoneBytes, err := yaml.Marshal(filedata[`zoneconfig`]) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + zoneDataStruct := zoneConfig_1_0_0{} |
| 157 | + |
| 158 | + if err = yaml.Unmarshal(zoneBytes, &zoneDataStruct); err != nil { |
| 159 | + return err |
| 160 | + } |
| 161 | + |
| 162 | + if filedata[`zone`] != nil { |
| 163 | + if zoneName, ok := filedata[`zone`].(string); ok { |
| 164 | + zoneDataStruct.Name = zoneName |
| 165 | + } else { |
| 166 | + zoneDataStruct.Name = filedata[`title`].(string) |
| 167 | + } |
| 168 | + |
| 169 | + if defaultBiome, ok := filedata[`biome`].(string); ok { |
| 170 | + zoneDataStruct.DefaultBiome = defaultBiome |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + mudlog.Info("Migration 0.9.1", "file", path, "message", "writing "+zoneFilePath) |
| 175 | + |
| 176 | + // |
| 177 | + // Write the zone data to the zone-config.yaml path |
| 178 | + // We'll just use whatever permissions were set in the room file for this file. |
| 179 | + // |
| 180 | + zoneFileBytes, err := yaml.Marshal(zoneDataStruct) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + if err := os.WriteFile(zoneFilePath, zoneFileBytes, roomFileInfo.Mode().Perm()); err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + // Mark zone file as existing |
| 189 | + existingZoneFiles[zoneFilePath] = struct{}{} |
| 190 | + |
| 191 | + mudlog.Info("Migration 0.9.1", "file", path, "message", "writing modified room data") |
| 192 | + |
| 193 | + // |
| 194 | + // Now clear the "zoneconfig" node from the room data. |
| 195 | + // The data will be in a random order if we just write this back to the room yaml file, |
| 196 | + // so we'll take the extract step of marshalling the room data from the map into a string, |
| 197 | + // and then unmarshal it into the actual target rooms.Room{} struct. |
| 198 | + // This way, when writing to a file, it'll be in the typical field order according to the struct |
| 199 | + // field order. |
| 200 | + // |
| 201 | + delete(filedata, `zoneconfig`) |
| 202 | + |
| 203 | + // First marshal the modified room data into bytes |
| 204 | + modifiedRoomBytes, err := yaml.Marshal(filedata) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + |
| 209 | + // Unmarshal the bytes into the proper struct |
| 210 | + modifiedRoomStruct := rooms.Room{} |
| 211 | + if err = yaml.Unmarshal(modifiedRoomBytes, &modifiedRoomStruct); err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + |
| 215 | + // Marshal again, this time using the proper struct |
| 216 | + modifiedRoomBytes, err = yaml.Marshal(modifiedRoomStruct) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + |
| 221 | + // Again, we'll just use the rooms original permissions when writing. |
| 222 | + if err := os.WriteFile(path, modifiedRoomBytes, roomFileInfo.Mode().Perm()); err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + mudlog.Info("Migration 0.9.1", "file", path, "message", "successfully updated") |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | + return nil |
| 231 | +} |
0 commit comments