Skip to content

Commit fa689a8

Browse files
committed
Clean up save projection helpers
1 parent 08c7129 commit fa689a8

6 files changed

Lines changed: 244 additions & 221 deletions

File tree

backend/cmd/server/native_port_projection.go

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ type nativePortProjectionAdapter struct {
1919
N64CanonicalToPort func(saveSummary, []byte) (string, []byte, error)
2020
}
2121

22+
type nativePortOriginKey struct {
23+
SystemSlug string
24+
TitleKey string
25+
MediaType string
26+
}
27+
2228
var nativePortProjectionAdapters = []nativePortProjectionAdapter{
2329
{
2430
PortID: "ship-of-harkinian",
@@ -74,34 +80,79 @@ var nativePortProjectionAdapters = []nativePortProjectionAdapter{
7480
},
7581
}
7682

77-
func nativePortManifestForRuntimeProfile(profile string) (nativePortManifest, bool) {
78-
clean := strings.ToLower(strings.TrimSpace(profile))
79-
for _, manifest := range nativePortManifests {
80-
if strings.EqualFold(manifest.RuntimeProfile, clean) {
81-
return manifest, true
83+
var nativePortProjectionAdaptersByProfile = func() map[string]nativePortProjectionAdapter {
84+
out := make(map[string]nativePortProjectionAdapter, len(nativePortProjectionAdapters))
85+
for _, adapter := range nativePortProjectionAdapters {
86+
out[nativePortProfileKey(adapter.RuntimeProfile)] = adapter
87+
}
88+
return out
89+
}()
90+
91+
var nativePortProjectionAdaptersByOrigin = func() map[nativePortOriginKey]nativePortProjectionAdapter {
92+
out := make(map[nativePortOriginKey]nativePortProjectionAdapter, len(nativePortProjectionAdapters))
93+
for _, adapter := range nativePortProjectionAdapters {
94+
if adapter.OriginSystemSlug == "n64" {
95+
out[nativePortOriginKeyForAdapter(adapter, adapter.CanonicalMediaType)] = adapter
96+
continue
8297
}
98+
if adapter.OriginIdentity {
99+
out[nativePortOriginKeyForAdapter(adapter, "")] = adapter
100+
}
101+
}
102+
return out
103+
}()
104+
105+
func nativePortProfileKey(profile string) string {
106+
return strings.ToLower(strings.TrimSpace(profile))
107+
}
108+
109+
func nativePortRuntimeProfileFromID(portID string) string {
110+
portID = canonicalOptionalSegment(portID)
111+
if portID == "" {
112+
return ""
113+
}
114+
return "port/" + portID
115+
}
116+
117+
func nativePortOriginKeyForAdapter(adapter nativePortProjectionAdapter, mediaType string) nativePortOriginKey {
118+
return nativePortOriginKey{
119+
SystemSlug: canonicalSegment(adapter.OriginSystemSlug, ""),
120+
TitleKey: canonicalTrackTitleKey(adapter.OriginDisplayTitle),
121+
MediaType: canonicalOptionalSegment(mediaType),
83122
}
84-
return nativePortManifest{}, false
123+
}
124+
125+
func nativePortOriginKeyForSummary(summary saveSummary, mediaType string) nativePortOriginKey {
126+
return nativePortOriginKey{
127+
SystemSlug: canonicalSegment(saveSummarySystemSlug(summary), ""),
128+
TitleKey: canonicalTrackTitleKey(firstNonEmpty(summary.DisplayTitle, summary.Game.DisplayTitle, summary.Game.Name)),
129+
MediaType: canonicalOptionalSegment(mediaType),
130+
}
131+
}
132+
133+
func nativePortManifestForRuntimeProfile(profile string) (nativePortManifest, bool) {
134+
manifest, ok := nativePortManifestsByRuntimeProfile[nativePortProfileKey(profile)]
135+
return manifest, ok
85136
}
86137

87138
func nativePortProjectionAdapterForProfile(profile string) (nativePortProjectionAdapter, bool) {
88-
clean := strings.ToLower(strings.TrimSpace(profile))
89-
for _, adapter := range nativePortProjectionAdapters {
90-
if strings.EqualFold(adapter.RuntimeProfile, clean) {
91-
return adapter, true
92-
}
139+
adapter, ok := nativePortProjectionAdaptersByProfile[nativePortProfileKey(profile)]
140+
return adapter, ok
141+
}
142+
143+
func nativePortProjectionAdapterForCandidates(runtimeProfile, portID string) (nativePortProjectionAdapter, bool) {
144+
if adapter, ok := nativePortProjectionAdapterForProfile(runtimeProfile); ok {
145+
return adapter, true
146+
}
147+
if adapter, ok := nativePortProjectionAdapterForProfile(nativePortRuntimeProfileFromID(portID)); ok {
148+
return adapter, true
93149
}
94150
return nativePortProjectionAdapter{}, false
95151
}
96152

97153
func nativePortProjectionAdapterForSummary(summary saveSummary) (nativePortProjectionAdapter, bool) {
98-
for _, candidate := range []string{
99-
summary.RuntimeProfile,
100-
"port/" + summary.PortID,
101-
} {
102-
if adapter, ok := nativePortProjectionAdapterForProfile(candidate); ok {
103-
return adapter, true
104-
}
154+
if adapter, ok := nativePortProjectionAdapterForCandidates(summary.RuntimeProfile, summary.PortID); ok {
155+
return adapter, true
105156
}
106157
return nativePortProjectionAdapterForOriginSummary(summary)
107158
}
@@ -114,13 +165,8 @@ func nativePortProjectionAdapterForN64Summary(summary saveSummary) (nativePortPr
114165
if !ok {
115166
return nativePortProjectionAdapter{}, false
116167
}
117-
titleKey := canonicalTrackTitleKey(firstNonEmpty(summary.DisplayTitle, summary.Game.DisplayTitle, summary.Game.Name))
118-
for _, adapter := range nativePortProjectionAdapters {
119-
if adapter.OriginSystemSlug == "n64" && adapter.CanonicalMediaType == info.MediaType && canonicalTrackTitleKey(adapter.OriginDisplayTitle) == titleKey {
120-
return adapter, true
121-
}
122-
}
123-
return nativePortProjectionAdapter{}, false
168+
adapter, ok := nativePortProjectionAdaptersByOrigin[nativePortOriginKeyForSummary(summary, info.MediaType)]
169+
return adapter, ok
124170
}
125171

126172
func nativePortProjectionAdapterForOriginSummary(summary saveSummary) (nativePortProjectionAdapter, bool) {
@@ -131,13 +177,8 @@ func nativePortProjectionAdapterForOriginSummary(summary saveSummary) (nativePor
131177
if systemSlug == "" || systemSlug == nativePortSystemSlug {
132178
return nativePortProjectionAdapter{}, false
133179
}
134-
titleKey := canonicalTrackTitleKey(firstNonEmpty(summary.DisplayTitle, summary.Game.DisplayTitle, summary.Game.Name))
135-
for _, adapter := range nativePortProjectionAdapters {
136-
if adapter.OriginSystemSlug == systemSlug && adapter.OriginIdentity && canonicalTrackTitleKey(adapter.OriginDisplayTitle) == titleKey {
137-
return adapter, true
138-
}
139-
}
140-
return nativePortProjectionAdapter{}, false
180+
adapter, ok := nativePortProjectionAdaptersByOrigin[nativePortOriginKeyForSummary(summary, "")]
181+
return adapter, ok
141182
}
142183

143184
func nativePortRuntimeProfilesForSummary(summary saveSummary) []runtimeProfileDefinition {
@@ -197,17 +238,11 @@ func storedOriginNativePortProfile(summary saveSummary) (string, bool) {
197238
if systemSlug == "" || systemSlug == nativePortSystemSlug {
198239
return "", false
199240
}
200-
for _, candidate := range []string{
201-
strings.TrimSpace(summary.RuntimeProfile),
202-
"port/" + strings.TrimSpace(summary.PortID),
203-
} {
204-
manifest, ok := nativePortManifestForRuntimeProfile(candidate)
205-
if !ok {
206-
continue
207-
}
208-
if manifest.OriginSystemSlug == systemSlug {
209-
return manifest.RuntimeProfile, true
210-
}
241+
if manifest, ok := nativePortManifestForRuntimeProfile(summary.RuntimeProfile); ok && manifest.OriginSystemSlug == systemSlug {
242+
return manifest.RuntimeProfile, true
243+
}
244+
if manifest, ok := nativePortManifestForRuntimeProfile(nativePortRuntimeProfileFromID(summary.PortID)); ok && manifest.OriginSystemSlug == systemSlug {
245+
return manifest.RuntimeProfile, true
211246
}
212247
return "", false
213248
}

backend/cmd/server/native_ports.go

Lines changed: 66 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ var nativePortManifests = map[string]nativePortManifest{
107107
},
108108
}
109109

110+
var nativePortManifestsByRuntimeProfile = func() map[string]nativePortManifest {
111+
out := make(map[string]nativePortManifest, len(nativePortManifests))
112+
for _, manifest := range nativePortManifests {
113+
out[nativePortProfileKey(manifest.RuntimeProfile)] = manifest
114+
}
115+
return out
116+
}()
117+
110118
func nativePortMetadataFromForm(formValue func(string) string, runtimeProfile string) (nativePortMetadata, bool) {
111119
if formValue == nil {
112120
return nativePortMetadata{}, false
@@ -125,24 +133,34 @@ func nativePortMetadataFromForm(formValue func(string) string, runtimeProfile st
125133
return meta, meta.PortID != "" || meta.RuntimeProfile != ""
126134
}
127135

136+
func completeNativePortMetadata(meta nativePortMetadata) nativePortMetadata {
137+
if meta.PortID == "" && meta.RuntimeProfile == "" {
138+
return meta
139+
}
140+
manifest, ok := nativePortManifestForMetadata(meta)
141+
if !ok {
142+
return meta
143+
}
144+
if meta.PortID == "" {
145+
meta.PortID = manifest.ID
146+
}
147+
if meta.PortName == "" {
148+
meta.PortName = manifest.Name
149+
}
150+
if meta.OriginSystemSlug == "" {
151+
meta.OriginSystemSlug = manifest.OriginSystemSlug
152+
}
153+
if meta.RuntimeProfile == "" {
154+
meta.RuntimeProfile = manifest.RuntimeProfile
155+
}
156+
return meta
157+
}
158+
128159
func applyNativePortInput(input saveCreateInput, meta nativePortMetadata) saveCreateInput {
129160
if meta.PortID == "" && meta.RuntimeProfile == "" {
130161
return input
131162
}
132-
if manifest, ok := nativePortManifestForMetadata(meta); ok {
133-
if meta.PortID == "" {
134-
meta.PortID = manifest.ID
135-
}
136-
if meta.PortName == "" {
137-
meta.PortName = manifest.Name
138-
}
139-
if meta.OriginSystemSlug == "" {
140-
meta.OriginSystemSlug = manifest.OriginSystemSlug
141-
}
142-
if meta.RuntimeProfile == "" {
143-
meta.RuntimeProfile = manifest.RuntimeProfile
144-
}
145-
}
163+
meta = completeNativePortMetadata(meta)
146164
input.PortID = meta.PortID
147165
input.PortName = meta.PortName
148166
input.OriginSystemSlug = meta.OriginSystemSlug
@@ -167,20 +185,7 @@ func attachNativePortMetadata(input saveCreateInput, meta nativePortMetadata) sa
167185
if meta.PortID == "" && meta.RuntimeProfile == "" {
168186
return input
169187
}
170-
if manifest, ok := nativePortManifestForMetadata(meta); ok {
171-
if meta.PortID == "" {
172-
meta.PortID = manifest.ID
173-
}
174-
if meta.PortName == "" {
175-
meta.PortName = manifest.Name
176-
}
177-
if meta.OriginSystemSlug == "" {
178-
meta.OriginSystemSlug = manifest.OriginSystemSlug
179-
}
180-
if meta.RuntimeProfile == "" {
181-
meta.RuntimeProfile = manifest.RuntimeProfile
182-
}
183-
}
188+
meta = completeNativePortMetadata(meta)
184189
input.PortID = firstNonEmpty(input.PortID, meta.PortID)
185190
input.PortName = firstNonEmpty(input.PortName, meta.PortName)
186191
input.OriginSystemSlug = firstNonEmpty(input.OriginSystemSlug, meta.OriginSystemSlug)
@@ -269,34 +274,13 @@ func titleizeNativePortSlot(value string) string {
269274
}
270275

271276
func validateNativePortSave(input saveCreateInput, detection saveSystemDetectionResult) consoleValidationResult {
272-
if !detection.Evidence.HelperTrusted && !metadataHasTrustedSystemEvidence(input.Metadata) {
273-
return consoleValidationResult{
274-
Rejected: true,
275-
RejectReason: "native port saves must come from a trusted helper manifest",
276-
}
277-
}
278277
meta := nativePortMetadataFromInput(input)
279278
manifest, ok := nativePortManifestForMetadata(meta)
280279
if !ok {
281280
return consoleValidationResult{Rejected: true, RejectReason: "unknown native port manifest"}
282281
}
283-
if canonicalOptionalSegment(meta.PortSaveKind) != "progress" {
284-
return consoleValidationResult{Rejected: true, RejectReason: "only native port progress saves are supported"}
285-
}
286-
if len(input.Payload) == 0 {
287-
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is empty"}
288-
}
289-
if len(input.Payload) > 16*1024*1024 {
290-
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is too large"}
291-
}
292-
if !nativePortPathAllowed(manifest, meta.RelativePath) {
293-
return consoleValidationResult{
294-
Rejected: true,
295-
RejectReason: fmt.Sprintf("native port path %q is not allowed for %s", meta.RelativePath, manifest.ID),
296-
}
297-
}
298-
if meta.RootRelativePath != "" && !safePortRelativePath(meta.RootRelativePath) {
299-
return consoleValidationResult{Rejected: true, RejectReason: "native port root-relative path is unsafe"}
282+
if result, ok := validateNativePortEnvelope(input, detection, meta, manifest); !ok {
283+
return result
300284
}
301285
return consoleValidationResult{
302286
Inspection: &saveInspection{
@@ -335,30 +319,9 @@ func originNativePortManifestForInput(input saveCreateInput, systemSlug string)
335319
}
336320

337321
func validateOriginNativePortSave(input saveCreateInput, detection saveSystemDetectionResult, manifest nativePortManifest) consoleValidationResult {
338-
if !detection.Evidence.HelperTrusted && !metadataHasTrustedSystemEvidence(input.Metadata) {
339-
return consoleValidationResult{
340-
Rejected: true,
341-
RejectReason: "native port saves must come from a trusted helper manifest",
342-
}
343-
}
344322
meta := nativePortMetadataFromInput(input)
345-
if canonicalOptionalSegment(meta.PortSaveKind) != "progress" {
346-
return consoleValidationResult{Rejected: true, RejectReason: "only native port progress saves are supported"}
347-
}
348-
if len(input.Payload) == 0 {
349-
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is empty"}
350-
}
351-
if len(input.Payload) > 16*1024*1024 {
352-
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is too large"}
353-
}
354-
if !nativePortPathAllowed(manifest, meta.RelativePath) {
355-
return consoleValidationResult{
356-
Rejected: true,
357-
RejectReason: fmt.Sprintf("native port path %q is not allowed for %s", meta.RelativePath, manifest.ID),
358-
}
359-
}
360-
if meta.RootRelativePath != "" && !safePortRelativePath(meta.RootRelativePath) {
361-
return consoleValidationResult{Rejected: true, RejectReason: "native port root-relative path is unsafe"}
323+
if result, ok := validateNativePortEnvelope(input, detection, meta, manifest); !ok {
324+
return result
362325
}
363326

364327
title := strings.TrimSpace(manifest.OriginGameTitle)
@@ -390,6 +353,34 @@ func validateOriginNativePortSave(input saveCreateInput, detection saveSystemDet
390353
}
391354
}
392355

356+
func validateNativePortEnvelope(input saveCreateInput, detection saveSystemDetectionResult, meta nativePortMetadata, manifest nativePortManifest) (consoleValidationResult, bool) {
357+
if !detection.Evidence.HelperTrusted && !metadataHasTrustedSystemEvidence(input.Metadata) {
358+
return consoleValidationResult{
359+
Rejected: true,
360+
RejectReason: "native port saves must come from a trusted helper manifest",
361+
}, false
362+
}
363+
if canonicalOptionalSegment(meta.PortSaveKind) != "progress" {
364+
return consoleValidationResult{Rejected: true, RejectReason: "only native port progress saves are supported"}, false
365+
}
366+
if len(input.Payload) == 0 {
367+
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is empty"}, false
368+
}
369+
if len(input.Payload) > 16*1024*1024 {
370+
return consoleValidationResult{Rejected: true, RejectReason: "native port save payload is too large"}, false
371+
}
372+
if !nativePortPathAllowed(manifest, meta.RelativePath) {
373+
return consoleValidationResult{
374+
Rejected: true,
375+
RejectReason: fmt.Sprintf("native port path %q is not allowed for %s", meta.RelativePath, manifest.ID),
376+
}, false
377+
}
378+
if meta.RootRelativePath != "" && !safePortRelativePath(meta.RootRelativePath) {
379+
return consoleValidationResult{Rejected: true, RejectReason: "native port root-relative path is unsafe"}, false
380+
}
381+
return consoleValidationResult{}, true
382+
}
383+
393384
func nativePortMetadataFromInput(input saveCreateInput) nativePortMetadata {
394385
return nativePortMetadata{
395386
PortID: strings.TrimSpace(input.PortID),
@@ -409,13 +400,7 @@ func nativePortManifestForMetadata(meta nativePortMetadata) (nativePortManifest,
409400
manifest, ok := nativePortManifests[meta.PortID]
410401
return manifest, ok
411402
}
412-
profile := strings.TrimSpace(meta.RuntimeProfile)
413-
for _, manifest := range nativePortManifests {
414-
if profile != "" && strings.EqualFold(manifest.RuntimeProfile, profile) {
415-
return manifest, true
416-
}
417-
}
418-
return nativePortManifest{}, false
403+
return nativePortManifestForRuntimeProfile(meta.RuntimeProfile)
419404
}
420405

421406
func nativePortPathAllowed(manifest nativePortManifest, relativePath string) bool {

0 commit comments

Comments
 (0)