This commit is contained in:
Robert Janetzko 2022-04-21 08:58:28 +00:00
parent b534159a8d
commit e136b2f8e6
3 changed files with 6641 additions and 1038 deletions

View File

@ -103,6 +103,14 @@ type {{ $obj.Name }} struct {
{{- end }} {{- end }}
} }
func New{{ $obj.Name }}() *{{ $obj.Name }} {
return &{{ $obj.Name }}{
{{- range $fname, $field := $obj.Fields }}{{- if $field.MustInit }}{{- if not ($field.SameField $obj) }}
{{ $field.Init }}
{{- end }}{{- end }}{{- end }}
}
}
{{- if $obj.Id }} {{- if $obj.Id }}
func (x *{{ $obj.Name }}) Id() int { return x.Id_ } func (x *{{ $obj.Name }}) Id() int { return x.Id_ }
{{- end }} {{- end }}
@ -135,6 +143,14 @@ func (x *{{ $obj.Name }}) CheckFields() {
{{- end }} {{- end }}
} }
func (x *{{ $obj.Name }}) MarshalJSON() ([]byte, error) {
d := make(map[string]any)
{{- range $fname, $field := $obj.Fields }}{{- if not ($field.SameField $obj) }}{{- if not (and (eq $fname "type") (not (not $obj.SubTypes))) }}
{{ $field.JsonMarshal }}
{{- end }}{{- end }}{{- end }}
return json.Marshal(d)
}
{{- end }} {{- end }}
// Parser // Parser
@ -142,21 +158,15 @@ func (x *{{ $obj.Name }}) CheckFields() {
{{- range $name, $obj := $.Objects }} {{- range $name, $obj := $.Objects }}
{{- range $plus := $.Modes }} {{- range $plus := $.Modes }}
func parse{{ $obj.Name }}{{ if $plus }}Plus{{ end }}(p *util.XMLParser{{ if $plus }}, obj *{{ $obj.Name }}{{ end }}) (*{{ $obj.Name }}, error) { func parse{{ $obj.Name }}{{ if $plus }}Plus{{ end }}(p *util.XMLParser{{ if $plus }}, obj *{{ $obj.Name }}{{ end }}) (*{{ $obj.Name }}, error) {
var ( {{- if not $plus }}
{{- if not $plus }} var obj = New{{ $obj.Name }}()
obj = &{{ $obj.Name }}{} {{- end }}
{{- end }}
)
{{- if $plus }} {{- if $plus }}
if obj == nil { if obj == nil {
obj = &{{ $obj.Name }}{} obj = New{{ $obj.Name }}()
} }
{{- end }} {{- end }}
{{- range $fname, $field := $obj.Fields }}
{{ $field.Init $plus }}
{{- end }}
for { for {
t, n, err := p.Token() t, n, err := p.Token()
if err != nil { if err != nil {
@ -295,9 +305,21 @@ func (f Field) TypeLine() string {
return fmt.Sprintf("%s %s%s %s", n, m, t, j) return fmt.Sprintf("%s %s%s %s", n, m, t, j)
} }
func (f Field) Init(plus bool) string { func (f Field) MustInit() bool {
if !plus && f.Type == "map" { return f.Type == "map" || (f.Type == "int" && !f.Multiple)
return fmt.Sprintf("obj.%s = make(map[int]*%s)", f.Name, *f.ElementType) }
func (f Field) Init() string {
n := f.Name
if n == "Id" || n == "Name" {
n = n + "_"
}
if f.Type == "map" {
return fmt.Sprintf("%s: make(map[int]*%s),", n, *f.ElementType)
}
if f.Type == "int" && !f.Multiple {
return fmt.Sprintf("%s: -1,", n)
} }
return "" return ""
@ -439,6 +461,22 @@ func (obj Object) LegendFields(t string) []Field {
return list return list
} }
func (f Field) JsonMarshal() string {
n := f.Name
if n == "Id" || n == "Name" {
n = n + "_"
}
if f.Type == "int" && !f.Multiple {
return fmt.Sprintf(`if x.%s != -1 { d["%s"] = x.%s }`, n, strcase.ToLowerCamel(f.Name), n)
}
if f.Type == "enum" && !f.Multiple {
return fmt.Sprintf(`if x.%s != 0 { d["%s"] = x.%s }`, n, strcase.ToLowerCamel(f.Name), n)
}
return fmt.Sprintf(`d["%s"] = x.%s`, strcase.ToLowerCamel(f.Name), n)
}
func (f Field) SameField(obj Object) bool { func (f Field) SameField(obj Object) bool {
if f.Legend != "plus" { if f.Legend != "plus" {
return false return false

View File

@ -135,7 +135,7 @@ func (x *HistoricalEventArtifactClaimFormed) Html() string {
case HistoricalEventArtifactClaimFormedClaim_Treasure: case HistoricalEventArtifactClaimFormedClaim_Treasure:
c := "" c := ""
if x.Circumstance != HistoricalEventArtifactClaimFormedCircumstance_Unknown { if x.Circumstance != HistoricalEventArtifactClaimFormedCircumstance_Unknown {
c = x.Circumstance.String() c = " " + x.Circumstance.String()
} }
if x.HistFigureId != -1 { if x.HistFigureId != -1 {
return a + " was claimed by " + hf(x.HistFigureId) + c return a + " was claimed by " + hf(x.HistFigureId) + c
@ -219,25 +219,64 @@ func (x *HistoricalEventArtifactGiven) Html() string {
} }
func (x *HistoricalEventArtifactLost) Html() string { func (x *HistoricalEventArtifactLost) Html() string {
w := "" w := ""
if x.SubregionId != -1 {
w = region(x.SubregionId)
}
if x.SiteId != -1 { if x.SiteId != -1 {
w = site(x.SiteId, "") w = site(x.SiteId, "")
} }
if x.SubregionId != -1 {
w = region(x.SubregionId) // TODO optional
}
return fmt.Sprintf("%s was lost in %s", artifact(x.ArtifactId), w) return fmt.Sprintf("%s was lost in %s", artifact(x.ArtifactId), w)
} }
func (x *HistoricalEventArtifactPossessed) Html() string { func (x *HistoricalEventArtifactPossessed) Html() string {
return "UNKNWON HistoricalEventArtifactPossessed" a := artifact(x.ArtifactId)
h := hf(x.HistFigureId)
w := ""
if x.SubregionId != -1 {
w = region(x.SubregionId)
}
if x.SiteId != -1 {
w = site(x.SiteId, "")
}
c := ""
switch x.Circumstance {
case HistoricalEventArtifactPossessedCircumstance_HfIsDead:
c = " after the death of " + hf(x.CircumstanceId)
}
switch x.Reason {
case HistoricalEventArtifactPossessedReason_ArtifactIsHeirloomOfFamilyHfid:
return fmt.Sprintf("%s was aquired in %s by %s as an heirloom of %s%s", a, w, h, hf(x.ReasonId), c)
case HistoricalEventArtifactPossessedReason_ArtifactIsSymbolOfEntityPosition:
return fmt.Sprintf("%s was aquired in %s by %s as a symbol of authority within %s%s", a, w, h, entity(x.ReasonId), c)
}
return fmt.Sprintf("%s was claimed in %s by %s%s", a, w, h, c) // TODO wording
} }
func (x *HistoricalEventArtifactRecovered) Html() string { func (x *HistoricalEventArtifactRecovered) Html() string {
return "UNKNWON HistoricalEventArtifactRecovered" a := artifact(x.ArtifactId)
h := hf(x.HistFigureId)
w := ""
if x.SubregionId != -1 {
w = "in " + region(x.SubregionId)
}
if x.SiteId != -1 {
w = site(x.SiteId, "in ")
if x.StructureId != -1 {
w = "from " + structure(x.SiteId, x.StructureId) + " " + w
}
}
return fmt.Sprintf("%s was recovered %s by %s", a, w, h)
} }
func (x *HistoricalEventArtifactStored) Html() string { return "UNKNWON HistoricalEventArtifactStored" }
func (x *HistoricalEventArtifactStored) Html() string {
return fmt.Sprintf("%s stored %s in %s", hf(x.HistFigureId), artifact(x.ArtifactId), site(x.SiteId, ""))
}
func (x *HistoricalEventArtifactTransformed) Html() string { func (x *HistoricalEventArtifactTransformed) Html() string {
return "UNKNWON HistoricalEventArtifactTransformed" return fmt.Sprintf("%s was made from %s by %s in %s", artifact(x.NewArtifactId), artifact(x.OldArtifactId), hf(x.HistFigureId), site(x.SiteId, "")) // TODO wording
} }
func (x *HistoricalEventAssumeIdentity) Html() string { return "UNKNWON HistoricalEventAssumeIdentity" } func (x *HistoricalEventAssumeIdentity) Html() string { return "UNKNWON HistoricalEventAssumeIdentity" }
func (x *HistoricalEventAttackedSite) Html() string { return "UNKNWON HistoricalEventAttackedSite" } func (x *HistoricalEventAttackedSite) Html() string { return "UNKNWON HistoricalEventAttackedSite" }
func (x *HistoricalEventBodyAbused) Html() string { return "UNKNWON HistoricalEventBodyAbused" } func (x *HistoricalEventBodyAbused) Html() string { return "UNKNWON HistoricalEventBodyAbused" }

File diff suppressed because it is too large Load Diff