Skip to content

Commit

Permalink
Allow setting the server icon via servermeta.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
dscalzi committed Aug 13, 2023
1 parent 11f8a72 commit 1a537e3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ Ex.
* This is a directory of toggleable modules. See the note below.
* `TestServer-1.12.2.png` Server icon file.

#### Setting the Server Icon

You can set the server icon in two ways.

1. __*(Preferred)*__ Place your server icon in the root server directory as shown in the example above. Only jpg and png files will be looked at. The name of the file does not matter.
2. Paste the **full** URL to your server icon in the servermeta.json for your server. It is highly recommended to only use files that are hosted on your own servers.

The value in servermeta.json will always be used so long as it is not empty and is a [valid url](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL). If it is empty or an [invalid url](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL), then the first method will be used.

#### Toggleable Modules

If a directory represents a toggleable mod, it will have three subdirectories. You must filter your files into these three.
Expand Down Expand Up @@ -279,6 +288,7 @@ Sample:
"version": "1.0.0",
"name": "Test (Minecraft 1.12.2)",
"description": "Test Running Minecraft 1.12.2 (Forge v14.23.5.2854)",
"icon": "How to set the server icon: https://github.com/dscalzi/Nebula#setting-the-server-icon",
"address": "localhost:25565",
"discord": {
"shortId": "1.12.2 Test Server",
Expand Down
2 changes: 2 additions & 0 deletions src/model/nebula/ServerMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function getDefaultServerMeta(id: string, version: string, options?: Serv
version: options?.version ?? '1.0.0',
name: `${id} (Minecraft ${version})`,
description: `${id} Running Minecraft ${version}`,
icon: 'How to set the server icon: https://github.com/dscalzi/Nebula#setting-the-server-icon',
address: 'localhost:25565',
discord: {
shortId: '<FILL IN OR REMOVE DISCORD OBJECT>',
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface ServerMeta {
version: Server['version']
name: Server['name']
description: Server['description']
icon?: Server['icon']
address: Server['address']
discord?: Server['discord']
mainServer: Server['mainServer']
Expand Down
37 changes: 24 additions & 13 deletions src/structure/spec_model/Server.struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MiscFileStructure } from './module/File.struct.js'
import { LibraryStructure } from './module/Library.struct.js'
import { MinecraftVersion } from '../../util/MinecraftVersion.js'
import { addSchemaToObject, SchemaTypes } from '../../util/SchemaUtil.js'
import { isValidUrl } from '../../util/StringUtils.js'

export interface CreateServerResult {
forgeModContainer?: string
Expand Down Expand Up @@ -123,26 +124,36 @@ export class ServerStructure extends BaseModelStructure<Server> {
continue
}

// Read server meta
const serverMeta = JSON.parse(await readFile(resolvePath(absoluteServerRoot, this.SERVER_META_FILE), 'utf-8')) as ServerMeta
const minecraftVersion = new MinecraftVersion(match[2])
const untrackedFiles: UntrackedFilesOption[] = serverMeta.untrackedFiles || []

let iconUrl: string = null!

// Resolve server icon
const subFiles = await readdir(absoluteServerRoot)
for (const subFile of subFiles) {
const caseInsensitive = subFile.toLowerCase()
if (caseInsensitive.endsWith('.jpg') || caseInsensitive.endsWith('.png')) {
iconUrl = new URL(join(relativeServerRoot, subFile), this.baseUrl).toString()

if(serverMeta.meta.icon && isValidUrl(serverMeta.meta.icon)) {
// Use the url they gave us.
iconUrl = serverMeta.meta.icon
} else {

this.logger.info('Server icon is either not set or not a valid URL.')
this.logger.info(`Looking for an icon file at ${absoluteServerRoot}`)

const subFiles = await readdir(absoluteServerRoot)
for (const subFile of subFiles) {
const caseInsensitive = subFile.toLowerCase()
if (caseInsensitive.endsWith('.jpg') || caseInsensitive.endsWith('.png')) {
iconUrl = new URL(join(relativeServerRoot, subFile), this.baseUrl).toString()
}
}
}

if (!iconUrl) {
this.logger.warn(`No icon file found for server ${file}.`)
if (!iconUrl) {
this.logger.warn(`No icon file found for server ${file}.`)
}
}

// Read server meta
const serverMeta = JSON.parse(await readFile(resolvePath(absoluteServerRoot, this.SERVER_META_FILE), 'utf-8')) as ServerMeta
const minecraftVersion = new MinecraftVersion(match[2])
const untrackedFiles: UntrackedFilesOption[] = serverMeta.untrackedFiles || []

const modules: Module[] = []

if(serverMeta.forge) {
Expand Down
9 changes: 9 additions & 0 deletions src/util/StringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ export function capitalize(str: string): string {
}
return str.charAt(0).toUpperCase() + str.slice(1)
}

export function isValidUrl(candidate: string): boolean {
try {
new URL(candidate)
return true
} catch (err) {
return false
}
}

0 comments on commit 1a537e3

Please sign in to comment.