Cram server and client functions together into the same file

It's how avorion does it anyway
This commit is contained in:
2025-07-20 18:30:17 +02:00
parent 766a5a9cfe
commit ef45b510e9
2 changed files with 28 additions and 11 deletions

View File

@@ -167,22 +167,21 @@ func ParseClass(file string) (*Class, error) {
}
className := strings.TrimSpace(class.Text())
// Clean up class name to be a valid Lua identifier
// Handle [Client], [Server] patterns as affixes
// Replace "Class [Client]" with "Class_Client"
// Replace "Class [Client] [Client] : Parent" with "Class_Client_Client_Parent"
// Merge [Client] and [Server] classes into single types
// Replace "Player [Client]" and "Player [Server]" with "Player"
// Replace "Alliance [Client]" and "Alliance [Server]" with "Alliance"
// First, handle the inheritance part (after ":")
// Remove inheritance part (after ":") - keep only the main class name
if strings.Contains(className, " : ") {
parts := strings.Split(className, " : ")
if len(parts) == 2 {
className = parts[0] + "_" + parts[1]
className = parts[0] // Keep only the main class name, ignore inheritance
}
}
// Handle [Client] and [Server] patterns
// Replace "[Client]" with "_Client"
className = strings.ReplaceAll(className, "[Client]", "_Client")
className = strings.ReplaceAll(className, "[Server]", "_Server")
// Remove [Client] and [Server] patterns to merge classes
className = strings.ReplaceAll(className, "[Client]", "")
className = strings.ReplaceAll(className, "[Server]", "")
// Remove any remaining brackets and clean up
className = strings.ReplaceAll(className, "[", "")
@@ -513,6 +512,12 @@ func getMethods(doc *goquery.Document, log *logger.Logger, className string) ([]
}
}
// Determine availability based on the original class name
availability := determineAvailability(originalClassName)
if availability != "" {
method.Comment = availability + " " + method.Comment
}
log.Trace("Method %s has %d parameters and %d return values", method.Name, len(method.Params), len(method.Returns))
res = append(res, method)
})
@@ -520,3 +525,15 @@ func getMethods(doc *goquery.Document, log *logger.Logger, className string) ([]
log.Debug("Found %d methods", len(res))
return res, nil
}
// determineAvailability returns a comment indicating whether a method is available on client, server, or both
func determineAvailability(originalClassName string) string {
if strings.Contains(originalClassName, "[Client]") && strings.Contains(originalClassName, "[Server]") {
return "[Client/Server]"
} else if strings.Contains(originalClassName, "[Client]") {
return "[Client]"
} else if strings.Contains(originalClassName, "[Server]") {
return "[Server]"
}
return ""
}

View File

@@ -19,9 +19,9 @@
---@return {{.Type}}{{if ne .Comment ""}} #{{truncateComment .Comment 80}}{{end}}
{{- end}}
{{- end}}
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,{{if ne $method.Comment ""}} -- {{truncateComment $method.Comment 80}}{{end}}
{{- else}}
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,
{{.Name}} = function(self{{if gt (len .Params) 0}}, {{range $index, $param := .Params}}{{if $index}}, {{end}}{{$param.Name}}{{end}}{{end}}) end,{{if ne $method.Comment ""}} -- {{truncateComment $method.Comment 80}}{{end}}
{{- end}}
{{- if ne (plus1 $index) $n}}