Skip to content

Instantly share code, notes, and snippets.

@Theaninova
Created October 21, 2020 13:23
Show Gist options
  • Save Theaninova/1515ce384036a98860629943f54ae4b3 to your computer and use it in GitHub Desktop.
Save Theaninova/1515ce384036a98860629943f54ae4b3 to your computer and use it in GitHub Desktop.
CS:GO Crosshair options parser
enum CrosshairStyle {
// DEFAULT = 1, // unsupported
DYNAMIC_MOV_SHOT_1 = 2, // pretty much all the same
DYNAMIC_MOV_SHOT_2 = 3,
FULL_STATIC = 4,
SEMI_STATIC = 5
}
/**
* Does not support the new Style CS Crosshair, no one uses it anyways
*/
interface CrosshairOptions {
style: CrosshairStyle,
tStyle: boolean,
// ignored crosshairGap_weaponValue: boolean,
/**
* Can only be in range 0 to 5
*/
crosshairColor: {
r: number,
g: number,
b: number,
a: number,
alphaEnabled: boolean,
},
/**
* Can be 0 (invisible)
*/
size: number,
/**
* Minimum 0.5
*/
thickness: number,
/**
* CS uses a default gap width, this has been taken care of
*/
gap: number,
dot: boolean,
stroke: {
enabled: boolean,
thickness: number,
}
}
function parseCSCrosshairOptions(options: string): CrosshairOptions {
const out: CrosshairOptions = {
style: CrosshairStyle.FULL_STATIC,
tStyle: false,
crosshairColor: {
r: 255,
g: 0,
b: 0,
a: 255,
alphaEnabled: false,
},
dot: false,
gap: 0,
size: 4,
thickness: 0.5,
stroke: {
enabled: false,
thickness: 0
}
};
for (const option of options.split(/\s*[;\n]+\s*/g)) {
const value: number | undefined = parseFloat(option.match(/(?<=")-?\d+.?\d*(?=")/)?.[0] ?? "")
const parameter_name: string | undefined = option.match(/cl_[\w_]+(?=\s+")/)?.[0];
if (typeof value == 'undefined' || typeof parameter_name == 'undefined') {
continue;
}
switch (parameter_name) {
case 'cl_crosshairstyle':
if (value == 2) out.style = CrosshairStyle.DYNAMIC_MOV_SHOT_1;
else if (value == 3) out.style = CrosshairStyle.DYNAMIC_MOV_SHOT_2;
else if (value == 4) out.style = CrosshairStyle.FULL_STATIC;
else if (value == 5) out.style = CrosshairStyle.SEMI_STATIC;
case 'cl_crosshaircolor': if (value > 1 && value <= 5) out.style = value; break;
case 'cl_crosshair_t': if (value == 0) out.tStyle = false; else if (value == 1) out.tStyle = true; break;
case 'cl_crosshair_t':
if (value == 1) { // green
out.crosshairColor.r = 0; out.crosshairColor.g = 255; out.crosshairColor.b = 0;
} else if (value == 2) { // yellow
out.crosshairColor.r = 255; out.crosshairColor.g = 255; out.crosshairColor.b = 0;
} else if (value == 3) { // blue
out.crosshairColor.r = 0; out.crosshairColor.g = 0; out.crosshairColor.b = 255;
} else if (value == 4) { // cyan
out.crosshairColor.r = 0; out.crosshairColor.g = 255; out.crosshairColor.b = 255;
} break;
case 'cl_crosshaircolor_r': if (value >= 0 && value <= 255) out.crosshairColor.r = value; break;
case 'cl_crosshaircolor_g': if (value >= 0 && value <= 255) out.crosshairColor.g = value; break;
case 'cl_crosshaircolor_b': if (value >= 0 && value <= 255) out.crosshairColor.b = value; break;
case 'cl_crosshairsize': out.size = value; break;
case 'cl_crosshairthickness': if (value >= 0.5) out.thickness = value; break;
case 'cl_crosshairgap': out.gap = value + 3; break; // correct for cs default gap. It's weird, I know.
case 'cl_crosshairusealpha': if (value == 1) out.crosshairColor.alphaEnabled = true; else if (value == 0) out.crosshairColor.alphaEnabled = false; break;
case 'cl_crosshairalpha': if (value >= 0 && value <= 255) out.crosshairColor.a = value; break;
case 'cl_crosshairdot': if (value == 1) out.dot = true; else if (value == 0) out.dot = false; break;
case 'cl_crosshair_drawoutline': if (value == 1) out.stroke.enabled = true; else if (value == 0) out.stroke.enabled = false; break;
case 'cl_crosshair_outlinethickness': if (value >= 0.5) out.stroke.thickness = value; break;
}
}
return out;
}
function toCSCrosshairOptions(options: CrosshairOptions): string {
return `
cl_crosshairstyle "${options.style.toString()}";
cl_crosshair_t "${options.tStyle ? 1 : 0}";
cl_crosshaircolor "1";
cl_crosshaircolor_r "${options.crosshairColor.r.toString()}";
cl_crosshaircolor_g "${options.crosshairColor.g.toString()}";
cl_crosshaircolor_b "${options.crosshairColor.b.toString()}";
cl_crosshairsize "${options.size.toString()}";
cl_crosshairthickness "${options.thickness.toString()}";
cl_crosshairgap "${(options.gap - 3).toString()}";
cl_crosshairusealpha "${options.crosshairColor.alphaEnabled ? 1 : 0}";
cl_crosshairalpha "${options.crosshairColor.a.toString()}";
cl_crosshairdot "${options.dot ? 1 : 0}";
cl_crosshair_drawoutline "${options.stroke.enabled ? 1 : 0}";
cl_crosshair_outlinethickness "${options.stroke.thickness.toString()}";
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment