Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source generation; move model and part definitions to yaml files #94

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Build output
out/
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"ISInventoryPaneContextMenu",
"ProceduralDistributions",
"SandboxVars"
]
],
"yaml.schemas": {
"./schema/coavinsmodel.json": ".coavinsmodel.yml",
}
}
10 changes: 10 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Make sure out directory exists
mkdir -p out

# Copy mod files to workshop directory
cp -r src out/coavinsfirearms

# Generate models lua from yaml source
lua build/write_models.lua def/CoavinsModels.yml out/coavinsfirearms/Contents/mods/coavinsfirearms/media/lua/client/coavinsfirearms/CoavinsModels.lua
61 changes: 61 additions & 0 deletions build/write_models.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- Script to generate source files

local yaml = require('yaml')

if #arg < 2 then
print("usage: lua " .. arg[0] .. " <ifile> <ofile>")
return
end

local ifile = arg[1]
local ofile = arg[2]

print("reading:", ifile)
print("writing:", ofile)

local f = io.open(ofile, "w+")
if not f then
print("unable to open file for writing")
return
end

-- set output file
io.output(f)

-- write warning message
io.write("-- This file was generated by write_models.lua and is not meant to be edited by hand\n")
io.write("-- If you want to define a new model in your own mod, just create a file like this and call the function below\n")

-- load yaml file
local y = yaml.loadpath(ifile)

-- copy the models into lua
for _, data in ipairs(y) do

local s = 'CoavinsFirearms.AddOrReplaceModel('

-- Param 1 - model name
s = s .. "'" .. data.model .. "'"

-- Param 2 - breaks into
s = s .. ", { '" .. data.receiver .. "'"
for _,part in ipairs(data.parts) do
s = s .. ", '" .. part .. "'"
end
s = s .. " }"

-- Param 3 - store type in
s = s .. ", '" .. data.receiver .. "'"

-- Param 4 - fallback type
if data.fallback then
s = s .. ", '" .. data.fallback .. "'"
end

s = s .. ")\n"

io.write(s)

end

io.close(f)
121 changes: 121 additions & 0 deletions def/all.coavinsmodel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# yaml-language-server: $schema=../schema/coavinsmodel.json

- model: GenericPistol
primaryComponent: PistolReceiver
requiredComponents:
- PistolSlide
defaultItem: Base.Pistol

- model: GenericRevolver
primaryComponent: RevolverReceiver
requiredComponents:
- RevolverCylinder
defaultItem: Base.Revolver

- model: GenericShotgun
primaryComponent: ShotgunReceiver
requiredComponents:
- ShotgunBarrel
defaultItem: Base.Shotgun

- model: M16Rifle
primaryComponent: M16LowerReceiver
requiredComponents:
- M16UpperReceiver
defaultItem: Base.AssaultRifle

- model: BoltActionRifle
primaryComponent: BoltActionReceiver
requiredComponents:
- BoltActionBolt
defaultItem: Base.VarmintRifle

- model: SKS
primaryComponent: SKS_Receiver
requiredComponents:
- SKS_BoltCarrier

- model: AK47
primaryComponent: AK47_Receiver
requiredComponents:
- AK47_BoltCarrier

- model: Pistol_45acp
primaryComponent: PistolReceiver_45acp
requiredComponents:
- PistolSlide_45acp

- model: Pistol_9mm
primaryComponent: PistolReceiver_9mm
requiredComponents:
- PistolSlide_9mm

- model: Shotgun_10g
primaryComponent: ShotgunReceiver_10g
requiredComponents:
- ShotgunBarrel_10g

- model: Shotgun_12g
primaryComponent: ShotgunReceiver_12g
requiredComponents:
- ShotgunBarrel_12g

- model: Pistol_22lr
primaryComponent: PistolReceiver_22lr
requiredComponents:
- PistolSlide_22lr

- model: Revolver_45lc
primaryComponent: RevolverReceiver_45lc
requiredComponents:
- RevolverCylinder_45lc

- model: Rifle_308AR
primaryComponent: RifleLowerReceiver_308AR
requiredComponents:
- RifleUpperReceiver_308AR

- model: Rifle_308BA
primaryComponent: RifleLowerReceiver_308BA
requiredComponents:
- RifleUpperReceiver_308BA

- model: Pistol_380acp
primaryComponent: PistolReceiver_380acp
requiredComponents:
- PistolSlide_380acp

- model: Rifle_556
primaryComponent: RifleLowerReceiver_556
requiredComponents:
- RifleUpperReceiver_556

- model: Revolver_357
primaryComponent: RevolverReceiver_357
requiredComponents:
- RevolverCylinder_357

- model: Pistol_44cal
primaryComponent: PistolReceiver_44cal
requiredComponents:
- PistolSlide_44cal

- model: Revolver_38spc
primaryComponent: RevolverReceiver_38spc
requiredComponents:
- RevolverCylinder_38spc

- model: Shotgun_4g
primaryComponent: ShotgunReceiver_4g
requiredComponents:
- ShotgunBarrel_4g

- model: Shotgun_20g
primaryComponent: ShotgunReceiver_20g
requiredComponents:
- ShotgunBarrel_20g

- model: Shotgun_410g
primaryComponent: ShotgunReceiver_410g
requiredComponents:
- ShotgunBarrel_410g
1 change: 1 addition & 0 deletions def/all.coavinspart.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# yaml-language-server: $schema=../schema/coavinspart.json
5 changes: 5 additions & 0 deletions docs/def.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# def/

The files in this folder are used to generate lua source files. This is done to keep information in one place and save time maintaining lots of different source files.

The `write_models.lua` script can read a yaml file from this folder and generate source files elsewhere.
38 changes: 38 additions & 0 deletions schema/coavinsmodel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Coavins Firearms Models",
"description": "Models file for Coavins Firearms, a Project Zomboid mod",
"type": "array",
"items": {
"type": "object",
"properties": {
"model": {
"type": "string",
"description": "The name of the model",
"examples": ["Pistol_45acp", "Shotgun_10g"]
},
"primaryComponent": {
"type": "string",
"description": "The primary part for this model. When the firearm is disassembled, its specific type will be saved within this part. This is done because multiple firearms may share the same model, and we need to know how to reconstitute the original firearm if the parts are combined again later. The receiver or frame is typically used in this role.",
"examples": ["PistolReceiver_45acp", "ShotgunReceiver_10g"]
},
"requiredComponents": {
"type": "array",
"description": "The subsidiary parts for this model. When assembled together with the primary, the player will create a firearm. The player must provide all required components, including the primary component, or the firearm cannot be assembled.\nCurrently only one part can be included here.",
"minItems": 1,
"maxItems": 1,
"items": {
"type": "string"
}
},
"defaultItem": {
"type": ["string", "null"],
"description": "The default item that the player receives when carrying out assembly using a primary component that has no type saved on it. This can happen when the primary component was found through scavenging.\nIf this property is not defined, the player will instead receive a random firearm from among those which can be assembled using these parts."
},
"optionalComponents": {
"type": ["array", "null"],
"description": "Optional parts for this model. Not currently used."
}
}
}
}
12 changes: 12 additions & 0 deletions schema/coavinspart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Coavins Firearms Part",
"description": "Part file for Coavins Firearms, a Project Zomboid mod",
"type": "array",
"items": {
"type": "object",
"properties": {

}
}
}

This file was deleted.

Loading