Skip to content

Commit

Permalink
init: added gpu id validator
Browse files Browse the repository at this point in the history
Since the GPU id selector is a very important argument, we should
add its validatior function in. Hence, let's do this.

This patch adds GPU ID validator into init/ directory.

Co-authored-by: Shuralyov, Jean <jean.shuralyov@proton.me>
Co-authored-by: Galyna, Cory <cory.galyna@gmail.com>
Co-authored-by: (Holloway) Chew, Kean Ho <hollowaykeanho@gmail.com>
Signed-off-by: (Holloway) Chew, Kean Ho <hollowaykeanho@gmail.com>
  • Loading branch information
3 people committed Feb 9, 2024
1 parent 921c875 commit d3a48a1
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 24 deletions.
100 changes: 96 additions & 4 deletions init/services/compilers/upscaler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,93 @@



function UPSCALER-Is-Available {
if (-not (OS-Host-System -eq "windows")) {
function UPSCALER-GPU-Scan {
# validate input
$___program = UPSCALER-Program-Get
if ((STRINGS-Is-Empty "${___program}") -eq 0) {
return ""
}


# create a dummy PNG file for upscale
$___filename = "${env:UPSCALER_PATH_ROOT}\.dummy.png"
$___target = "${env:UPSCALER_PATH_ROOT}\.dummy-upscaled.png"
$___header = @(
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4,
0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41,
0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x74, 0x52,
0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00,
0x00, 0x00, 0x04, 0x67, 0x41, 0x4D, 0x41, 0x00,
0x00, 0xAF, 0xC8, 0x37, 0x05, 0x8A, 0xE9, 0x00,
0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE,
0x42, 0x60, 0x82
)
$___bytes = [byte[]]$___header
$null = FS-Remove-Silently "${___filename}"
$null = FS-Remove-Silently "${___target}"
[System.IO.File]::WriteAllBytes($___filename, $___bytes)
$___result = & $___program -s 1 -i $___filename -o $___target 2>&1
$null = FS-Remove-Silently "${___filename}"
$null = FS-Remove-Silently "${___target}"
return $___result
}




function UPSCALER-GPU-Verify {
param(
[string]$___gpu
)


# validate input
if ((STRINGS-Is-Empty "${___gpu}") -eq 0) {
return 1
}

if (-not (OS-Host-Arch -eq "amd64")) {

# execute
$___term = UPSCALER-GPU-Scan
$___verdict = $false
$___lines = $___term -split "`n"
foreach ($___line in $___lines) {
if ($___line -notmatch '^\[') {
continue
}

$___line = $___line -replace '\]\s*$', ''
$___line = $___line -replace '^\[\s*', ''
$___id = $___line.Split(' ', 2)[0]

if ($___gpu -eq $___id) {
$___verdict = $true
break
}
}


# report status
if ($___verdict) {
return 0
}

return 1
}




function UPSCALER-Is-Available {
$___process = UPSCALER-Program-Get
if ((STRINGS-Is-Empty "${___process}") -eq 0) {
return 1
}

$___process = FS-Is-Target-Exist "${env:UPSCALER_PATH_ROOT}/bin/windows-amd64.exe"
$___process = FS-Is-Target-Exist "${___process}"
if ($___process -ne 0) {
return 1
}
Expand Down Expand Up @@ -115,6 +192,21 @@ function UPSCALER-Model-Get {



function UPSCALER-Program-Get {
if (-not (OS-Host-System -eq "windows")) {
return ""
}

if (-not (OS-Host-Arch -eq "amd64")) {
return ""
}

return "${env:UPSCALER_PATH_ROOT}/bin/windows-amd64.exe"
}




function UPSCALER-Scale-Get {
param(
[string]$___limit,
Expand Down
133 changes: 113 additions & 20 deletions init/services/compilers/upscaler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,87 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. "${LIBS_UPSCALER}/services/io/os.sh"
. "${LIBS_UPSCALER}/services/io/fs.sh"
. "${LIBS_UPSCALER}/services/io/strings.sh"




UPSCALER_Is_Available() {
case "$(OS_Host_System)" in
linux)
___program="${UPSCALER_PATH_ROOT}/bin/linux"
;;
darwin)
___program="${UPSCALER_PATH_ROOT}/bin/mac"
;;
*)
UPSCALER_GPU_Scan() {
# validate input
___program="$(UPSCALER_Program_Get)"
if [ "$(STRINGS_Is_Empty "$___program")" = "0" ]; then
printf -- ""
return 1
;;
esac
fi


case "$(OS_Host_Arch)" in
amd64)
___program="${___program}-amd64"
;;
*)
# Create a dummy PNG file for upscale
___filename="${UPSCALER_PATH_ROOT}/.dummy.png"
___target="${UPSCALER_PATH_ROOT}/.dummy-upscaled.png"
___header='\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4\x89\x00\x00\x00\x0aIDAT\x08\x96\x96\x96\x96\x00\x00\x00\x00\x01\x74\x52\x47\x42\x00\xae\xce\x1c\xe9\x00\x00\x00\x00IEND\xaeB`\x82'
FS_Remove_Silently "$___filename"
FS_Remove_Silently "$___target"
dd if=/dev/zero bs=1 count=1 2>/dev/null \
| printf '%b' "$___header" \
| cat > "${___filename}"
printf -- "%b" "$(2>&1 "${___program}" -s 1 -i "$___filename" -o "$___target")"
FS_Remove_Silently "$___filename"
FS_Remove_Silently "$___target"


# report status
return 0
}




UPSCALER_GPU_Verify() {
#___gpu="$1"


# validate input
if [ "$(STRINGS_Is_Empty "$1")" = "0" ]; then
return 1
;;
esac
fi


FS_Is_Target_Exist "$___program"
if [ $? -ne 0 ]; then
# execute
___term="$(UPSCALER_GPU_Scan)"
___verdict="1"
___old_IFS="$IFS"
while IFS="" read -r __line || [ -n "$__line" ]; do
if [ ! "$(printf -- "%.1b" "$__line")" = "[" ]; then
continue
fi

__line="${__line%%] *}"
__line="${__line#*[}"
if [ "$1" = "${__line%% *}" ]; then
___verdict="0"
break
fi
done <<EOF
${___term}
EOF
IFS="$___old_IFS" && unset ___old_IFS


# report status
if [ "$___verdict" = "0" ]; then
return 0
fi

return 1
}




UPSCALER_Is_Available() {
# execute
___program="$(UPSCALER_Program_Get)"
if [ "$(STRINGS_Is_Empty "$___program")" = "0" ]; then
return 1
fi

Expand Down Expand Up @@ -113,6 +164,48 @@ UPSCALER_Model_Get() {



UPSCALER_Program_Get() {
# execute
case "$(OS_Host_System)" in
linux)
___program="${UPSCALER_PATH_ROOT}/bin/linux"
;;
darwin)
___program="${UPSCALER_PATH_ROOT}/bin/mac"
;;
*)
printf -- ""
return 1
;;
esac


case "$(OS_Host_Arch)" in
amd64)
___program="${___program}-amd64"
;;
*)
printf -- ""
return 1
;;
esac


FS_Is_Target_Exist "$___program"
if [ $? -ne 0 ]; then
printf -- ""
return 1
fi
printf -- "%b" "$___program"


# report status
return 0
}




UPSCALER_Scale_Get() {
#___limit="$1"
#___input="$2"
Expand Down
55 changes: 55 additions & 0 deletions init/services/i18n/error-gpu-unsupported.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# BSD 3-Clause License
#
# Copyright (c) 2024 (Holloway) Chew, Kean Ho
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. "${env:LIBS_UPSCALER}\services\i18n\__printer.ps1"




function I18N-Status-Error-GPU-Unsupported {
param(
[string]$___gpu
)


# execute
switch (${env:UPSCALER_LANG}) {
{ $_ -in "DE", "de" } {
# german
$null = I18N-Status-Print "error" `
"Nicht unterstütztes GPU-Ziel: ${___gpu}`n"
} default {
# fallback to default english
$null = I18N-Status-Print "error" "Unsupported GPU target: ${___gpu}`n"
}}


# report status
return 0
}
54 changes: 54 additions & 0 deletions init/services/i18n/error-gpu-unsupported.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# BSD 3-Clause License
#
# Copyright (c) 2024 (Holloway) Chew, Kean Ho
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
. "${LIBS_UPSCALER}/services/i18n/__printer.sh"




I18N_Status_Error_GPU_Unsupported() {
#___gpu="$1"


# execute
case "$UPSCALER_LANG" in
DE|de)
# German
I18N_Status_Print "error" "Nicht unterstütztes GPU-Ziel: ${1}\n"
;;
*)
# fallback to default english
I18N_Status_Print "error" "Unsupported GPU target: ${1}\n"
;;
esac


# report status
return 0
}
Loading

0 comments on commit d3a48a1

Please sign in to comment.