-
-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Random Seed node * Added migration * Rebrand to Derive Seed * Made seeds their own datatype * Added test for migration
- Loading branch information
1 parent
da6bae0
commit a001d93
Showing
17 changed files
with
950 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from __future__ import annotations | ||
|
||
import hashlib | ||
import struct | ||
from typing import Union | ||
|
||
from ...node_base import NodeBase, group | ||
from ...node_factory import NodeFactory | ||
from ...properties.inputs import BaseInput, SeedInput | ||
from ...properties.outputs import SeedOutput | ||
from ...utils.seed import Seed | ||
from ...utils.utils import ALPHABET | ||
from . import category as UtilityCategory | ||
|
||
Source = Union[int, float, str, Seed] | ||
|
||
|
||
def SourceInput(label: str): | ||
return BaseInput( | ||
kind="generic", | ||
label=label, | ||
input_type="number | string | Directory | Seed", | ||
).make_optional() | ||
|
||
|
||
def _to_bytes(s: Source) -> bytes: | ||
if isinstance(s, str): | ||
return s.encode(errors="backslashreplace") | ||
if isinstance(s, Seed): | ||
s = s.value | ||
|
||
i = int(s) | ||
if isinstance(s, int) or s == i: | ||
return i.to_bytes(i.bit_length() // 8 + 1, byteorder="big", signed=True) | ||
|
||
return struct.pack("d", s) | ||
|
||
|
||
@NodeFactory.register("chainner:utility:derive_seed") | ||
class RandomNumberNode(NodeBase): | ||
def __init__(self): | ||
super().__init__() | ||
self.description = "Creates a new seed from multiple sources of randomness." | ||
self.inputs = [ | ||
group("seed")(SeedInput(has_handle=False)), | ||
SourceInput(f"Source A"), | ||
group("optional-list")( | ||
*[SourceInput(f"Source {letter}") for letter in ALPHABET[1:10]], | ||
), | ||
] | ||
self.outputs = [ | ||
SeedOutput(), | ||
] | ||
|
||
self.category = UtilityCategory | ||
self.name = "Derive Seed" | ||
self.icon = "MdCalculate" | ||
self.sub = "Random" | ||
|
||
def run(self, seed: Seed, *sources: Source | None) -> Seed: | ||
h = hashlib.sha256() | ||
|
||
h.update(_to_bytes(seed)) | ||
for s in sources: | ||
if s is not None: | ||
h.update(_to_bytes(s)) | ||
|
||
return Seed.from_bytes(h.digest()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.