Skip to content

Commit

Permalink
Merge version 0.4.0 from Fatboychummy-CC/develop
Browse files Browse the repository at this point in the history
Version 0.4.0
  • Loading branch information
fatboychummy authored Mar 4, 2024
2 parents c89bed5 + fcd9c95 commit f4418e2
Show file tree
Hide file tree
Showing 15 changed files with 732 additions and 299 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
crafting_plan.txt
lib/basalt.lua
data
data
bruh
generaty.lua
links.txt
test.txt
xorify.lua
extern-lls
24 changes: 19 additions & 5 deletions helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@
package.path = package.path .. ";lib/?.lua;lib/?/init.lua"

local function main()
-- Initial setup: Load everything
local machines_common = require "ui.machines.common"
machines_common.load()
local load_ok, err = pcall(function()
-- Initial setup: Load everything
local machines_common = require "ui.machines.common"
machines_common.load()

local items_common = require "ui.items.common"
items_common.load()
local items_common = require "ui.items.common"
items_common.load()

local recipe_handler = require "recipe_handler"
recipe_handler.load()
end)

if not load_ok then
printError("Error while loading data:", err)

print("Checking if data can be fixed...")

require "data_fixer_upper".check()
return
end

require "data_fixer_upper".check()

Expand Down
255 changes: 217 additions & 38 deletions lib/data_fixer_upper.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local file_helper = require "file_helper":instanced("data")
local recipe_handler = require "recipe_handler"
local machines_common = require "ui.machines.common"
local items_common = require "ui.items.common"

local fixer_upper = {}

local fixes = {}

--- Fix duplicate machine names by determining which id the machines should "collapse" to
function fixer_upper.duplicate_machine_name_fixer()
function fixes.duplicate_machine_names()
print("Fixing duplicate machine names...")

---@type table<string, integer>
Expand Down Expand Up @@ -45,7 +48,7 @@ function fixer_upper.duplicate_machine_name_fixer()
end

--- Fix old style machine data (no id) to new style (with id). This also needs to update recipes to use the id instead of the name.
function fixer_upper.machine_id_fixer()
function fixes.machine_id_needed()
print("Fixing machine data...")

---@type table<integer, MachineData>
Expand Down Expand Up @@ -92,7 +95,7 @@ function fixer_upper.machine_id_fixer()
error(("Data fixer upper failed: No ID was generated for machine %s."):format(recipe.machine), 0)
end
recipe_count = recipe_count + 1
recipe_handler.edit_recipe(name, recipe.random_id, {
recipe_handler.edit_recipe(recipe.id, {
machine = machine_names_to_ids[recipe.machine]
})
end
Expand All @@ -107,7 +110,7 @@ end


--- Fix recipe data that uses machine names instead of IDs.
function fixer_upper.recipe_machine_fixer()
function fixes.recipe_machine_needed()
-- Update the recipes to use the new machine IDs.
print("Fixing recipe data...")

Expand All @@ -131,7 +134,7 @@ function fixer_upper.recipe_machine_fixer()
if not machine then
error(("Data fixer upper failed: No ID known for machine %s."):format(recipe.machine), 0)
end
recipe_handler.edit_recipe(name, recipe.random_id, {
recipe_handler.edit_recipe(recipe.id, {
machine = machine
})
end
Expand All @@ -143,6 +146,111 @@ function fixer_upper.recipe_machine_fixer()
recipe_handler.save()
end

--- Fix item data that is not generated.
function fixes.item_data_not_generated()
-- Search all recipes for items that don't have data.
-- We are looking for the following:
-- 1. recipe.id is nil
-- 1. a) recipe.random_id exists
-- 1. fix: if random_id exists, set id to random_id, otherwise generate a new id.
-- 2. recipe.result.id is nil
-- 2. a) recipe.result.name exists instead
-- 2. fix: if name exists, set id to the id of the item with that name -- we may need to generate a new id if the item doesn't exist.
-- 2. fail: if name doesn't exist, we can't fix this
-- 3. Check each ingredient, ingredient.id is nil
-- 3. a) ingredient.name exists
-- 3. fix: if name exists, set id to the id of the item with that name -- we may need to generate a new id if the item doesn't exist.
-- 3. fail: if name doesn't exist, we can't fix this

-- Ensure that items common loaded OK, if not, fail.
local loaded_ok, err = pcall(items_common.load)
if not loaded_ok then
error(("Data fixer upper failed: Cannot load items_common: %s"):format(err), 0)
end

-- Manually load the data from the file.
local lines = file_helper:get_lines(recipe_handler.SAVE_FILE)
local recipes = {}

for _, line in ipairs(lines) do
local recipe = textutils.unserialize(line) --[[@as Recipe?]]
table.insert(recipes, recipe)
end

-- Check for invalid data.
local recipe_count = 0

local used_recipe_ids = {} -- For manually generating IDs.

for i, recipe in ipairs(recipes) do
-- 1.
if not recipe.id then
recipe_count = recipe_count + 1
if recipe.random_id then
recipe.id = recipe.random_id

-- 1. a)
recipe.random_id = nil
else
repeat
recipe.id = math.random(-1000000, 1000000)
until not used_recipe_ids[recipe.id]
end

used_recipe_ids[recipe.id] = true
end

-- 2.
if not recipe.result.id then
recipe_count = recipe_count + 1
if recipe.result.name then
local id = items_common.get_item_id(recipe.result.name)
if id then
recipe.result.id = id
else
recipe.result.id = items_common.add_item(recipe.result.name)
end

-- 2. a)
recipe.result.name = nil
else
error(("Data fixer upper failed: Both ID and name are missing for recipe %d."):format(i), 0)
end
end

-- 3.
for j, ingredient in ipairs(recipe.ingredients) do
if not ingredient.id then
recipe_count = recipe_count + 1
if ingredient.name then
local id = items_common.get_item_id(ingredient.name)
if id then
ingredient.id = id
else
ingredient.id = items_common.add_item(ingredient.name)
end

-- 3. a)
ingredient.name = nil
else
error(("Data fixer upper failed: Both ID and name are missing for ingredient %d in recipe %d."):format(j, i), 0)
end
end
end
end

-- Save the data back to the file.
local serialized = {}
for _, recipe in ipairs(recipes) do
table.insert(serialized, textutils.serialize(recipe, {compact=true}))
end

file_helper:write(recipe_handler.SAVE_FILE, table.concat(serialized, "\n"))

-- Save the items data back to the file.
items_common.save()
end

local checks = {}

function checks.duplicate_machine_names()
Expand Down Expand Up @@ -177,29 +285,108 @@ function checks.recipe_machine_needed()
return false
end

--- Check if item data is not generated.
function checks.item_data_not_generated()
-- Search all recipes for items that don't have data.
-- We are looking for the following:
-- 1. recipe.id is nil
-- 1. a) recipe.random_id exists
-- 2. recipe.result.id is nil
-- 2. a) recipe.result.name exists instead
-- 3. Check each ingredient, ingredient.id is nil
-- 3. a) ingredient.name exists
--
-- We also want to strip out any extra data, so if recipe.id is not nil, but recipe.random_id exists (or recipe.name) we want to remove it.

-- Manually load the data from the file.
local lines = file_helper:get_lines(recipe_handler.SAVE_FILE)
local recipes = {}

for _, line in ipairs(lines) do
local recipe = textutils.unserialize(line) --[[@as Recipe?]]
table.insert(recipes, recipe)
end

-- Check for invalid data.
for _, recipe in ipairs(recipes) do
-- Missing cases
if not recipe.id or not recipe.result.id then
return true
end

-- Extra cases
if recipe.random_id or recipe.result.name then
return true
end

-- Ingredients...
for _, ingredient in ipairs(recipe.ingredients) do
-- Extra and missing case
if not ingredient.id or ingredient.name then
return true
end
end
end

return false
end

function fixer_upper.check()
term.clear()
term.setCursorPos(1, 1)
local duplicate_machine_names = checks.duplicate_machine_names()
local machine_id_needed = checks.machine_id_needed()
local recipe_machine_needed = checks.recipe_machine_needed()
local results = {}
local needs_run = false
for check_name, checker in pairs(checks) do
local out = checker()
results[check_name] = out
if out then
needs_run = true
end
end

if machine_id_needed or recipe_machine_needed or duplicate_machine_names then
if needs_run then
term.setTextColor(colors.white)
term.setCursorPos(1, 1)
term.clear()
print() -- Ensure the cursor is on the screen.

print("Data Fixer Upper needs to run, press 'q' to quit, or any other key to continue.")
print("Your data will be backed up.")
print("\nModules:")
if duplicate_machine_names then
print("- Duplicate machine names")
term.setTextColor(colors.yellow)
print(" Your data will be backed up.")

-- Check if any of the backup files exist already.

-- Machines common backup
if file_helper:exists(machines_common.BACKUP_FILE) then
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
write(" Warning: Machine data backup exists, and will be overwritten.")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()
end
if machine_id_needed then
print("- Machine IDs")

-- Recipe handler backup
if file_helper:exists(recipe_handler.BACKUP_FILE) then
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
write(" Warning: Recipe data backup exists, and will be overwritten.")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()
end
if recipe_machine_needed then
print("- Recipe machine IDs")

-- Items common backup
if file_helper:exists(items_common.BACKUP_FILE) then
term.setBackgroundColor(colors.red)
term.setTextColor(colors.white)
write(" Warning: Item data backup exists, and will be overwritten.")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
print()
end


print("\nModules:")
for check_name, result in pairs(results) do
term.setTextColor(result and colors.white or colors.gray)
print("-", check_name, result and "needs to be run." or "is OK.")
end
sleep() -- Ensure the event queue is cleared.
local ev, key = os.pullEvent("key")
Expand All @@ -213,25 +400,17 @@ function fixer_upper.check()
machines_common.backup_save()
recipe_handler.backup_save()

if duplicate_machine_names then
term.setTextColor(colors.orange)
print("Running machine ID fixer...")
for check_name, fixer in pairs(fixes) do
if results[check_name] then
term.setTextColor(colors.white)
print("Running", check_name, "fixer...")
term.setTextColor(colors.lightGray)

fixer_upper.machine_id_fixer()
end
fixer()

if machine_id_needed then
term.setTextColor(colors.orange)
print("Running machine ID fixer...")
term.setTextColor(colors.lightGray)
fixer_upper.machine_id_fixer()
end

if recipe_machine_needed then
term.setTextColor(colors.orange)
print("Running recipe machine fixer...")
term.setTextColor(colors.lightGray)
fixer_upper.recipe_machine_fixer()
term.setTextColor(colors.white)
print("Finished", check_name, "fixer.")
end
end

if checks.duplicate_machine_names() then
Expand Down
Loading

0 comments on commit f4418e2

Please sign in to comment.